class PhoneInfo {
fun getAppAllList(context: Context): ArrayList? {
val appList = ArrayList()
val packageManager = context.packageManager
val arrInfo = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)
if (arrInfo != null) {
for (i in arrInfo.indices) {
val pInfo = arrInfo[i]
val type = pInfo.applicationInfo.flags
val app_name = pInfo.applicationInfo.loadLabel(context.packageManager).toString()
appList.add(app_name)
}
return appList
}
return null
}
fun phoneDevices(): String {
return Build.VERSION.RELEASE
}
fun getPhone_model(): String {
return Build.MODEL
}
/**
* 获取版本名称
* @return 版本名称
*/
fun versionName(context: Context): String? { //获取包管理器
val pm = context.packageManager
//获取包信息
try {
val packageInfo = pm.getPackageInfo(context.packageName, 0)
//返回版本号
return packageInfo.versionName
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
}
return null
}
/**
* 获取屏幕分辨率
*
* @return
*/
fun getFenbianlv(mContext1: Context): String {
try {
val display = mContext1.resources
.displayMetrics
return JsonInit().dataToJson(display)
} catch (e: Exception) {
e.fillInStackTrace()
}
return ""
}
/**
* 屏幕尺寸
*/
private var size = 0.0
fun getPhoneSize(context: Activity): String {
try {
getPhones(context)
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return size.toString()
}
/**
*
* @param context
* @return
*/
@SuppressLint("MissingPermission")
fun imeiData(context: Context): String {
val manager =
context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
try {
val method: Method =
manager.javaClass.getMethod("getImei", Int::class.javaPrimitiveType)
val imei1 = method.invoke(manager, 0) as String
val imei2 = method.invoke(manager, 1) as String
if (TextUtils.isEmpty(imei2)) {
return imei1
}
if (!TextUtils.isEmpty(imei1)) {
var imei = ""
imei = if (imei1 <= imei2) {
imei1 + ""
} else {
imei2 + ""
}
return imei
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
return ""
}
return ""
}
private fun getPhones(context: Activity) {
var x = 0
var y = 0
val display = context.windowManager.defaultDisplay
val metrics = DisplayMetrics()
display.getMetrics(metrics)
when {
Build.VERSION.SDK_INT >= 17 -> {
val size = Point()
display.getRealSize(size)
x = size.x
y = size.y
}
Build.VERSION.SDK_INT in 14..16 -> {
val mGetRawH =
Display::class.java.getMethod("getRawHeight")
val mGetRawW =
Display::class.java.getMethod("getRawWidth")
x = mGetRawW.invoke(display) as Int
y = mGetRawH.invoke(display) as Int
}
else -> {
x = metrics.widthPixels
y = metrics.heightPixels
}
}
size =
formatInit(
Math.sqrt(x / metrics.xdpi * (x / metrics.xdpi) + y / metrics.ydpi * (y / metrics.ydpi).toDouble()),
1
)
}
private fun formatInit(d: Double, newScale: Int): Double {
val bd = BigDecimal(d)
return bd.setScale(newScale, BigDecimal.ROUND_HALF_UP).toDouble()
}
/**
* 获取当前ip地址
*/
fun ipConfim(context: Context): String {
val info = (context
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager).activeNetworkInfo
if (info != null && info.isConnected) {
if (info.type == ConnectivityManager.TYPE_MOBILE) { // 当前使用2G/3G/4G网络
try {
val en =
NetworkInterface.getNetworkInterfaces()
while (en.hasMoreElements()) {
val enumIpAddr =
en.nextElement().inetAddresses
while (enumIpAddr.hasMoreElements()) {
val address = enumIpAddr.nextElement()
if (!address.isLoopbackAddress && address is Inet4Address) {
return address.getHostAddress()
}
}
}
} catch (e: SocketException) {
e.printStackTrace()
}
} else if (info.type == ConnectivityManager.TYPE_WIFI) { // 当前使用无线网络
val wifiProManager =
context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
val wifiProInfo = wifiProManager.connectionInfo
return getip(wifiProInfo.ipAddress)
}
} else { // 当前无网络连接,请在设置中打开网络
}
return ""
}
private fun getip(ip: Int): String {
return (ip and 0xFF).toString() + "." +
(ip shr 8 and 0xFF) + "." +
(ip shr 16 and 0xFF) + "." +
(ip shr 24 and 0xFF)
}
fun startTime(): Long {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
System.currentTimeMillis() - SystemClock.elapsedRealtimeNanos() / 1000000
} else System.currentTimeMillis()
}
fun isPhone(activity: Activity): Boolean {
val tm =
activity.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
return tm.phoneType == TelephonyManager.PHONE_TYPE_NONE
}
}
本文地址:https://blog.csdn.net/weixin_41194750/article/details/107185749