安卓常用工具方法

本文最后更新于:2024年12月24日 晚上

获取进程名字

【Android】获取当前进程名的四种方法及效率对比_android获取当前进程名-CSDN博客: https://blog.csdn.net/qq_23049111/article/details/126097443

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
fun getAppProcessName(): String {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
return Application.getProcessName()
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
try {
val processName =
Reflector.on("android.app.ActivityThread").method("currentProcessName")
.call<String>()
Log.d(TAG, "getAppProcessName: $processName")
return processName
} catch (e: Exception) {
Log.e(TAG, "getAppProcessName error", e)
return ""
}
} else {
try {
//安卓4.2的的currentPackageName实际上就是currentProcessName
//http://xrefandroid.com/android-4.2.2_r1/xref/frameworks/base/core/java/android/app/ActivityThread.java
val processName =
Reflector.on("android.app.ActivityThread").method("currentPackageName")
.call<String>()
Log.d(TAG, "getAppProcessName: $processName")
return processName
} catch (e: Exception) {
Log.e(TAG, "getAppProcessName error", e)
return ""
}
}
}

获取应用名称

Android 获取应用的icon、名字、版本号_查看安卓app类名-CSDN博客: https://blog.csdn.net/LoveDou0816/article/details/78135201

1
2
3
4
5
6
7
8
9
10
11
12
13
fun Context.getAppName(pkgName: String): String {
return try {
val info: PackageInfo = packageManager.getPackageInfo(pkgName, 0)
//resources.getString(info.applicationInfo.labelRes)
info.applicationInfo.loadLabel(packageManager).toString() //兼容android:label="${APP_NAME}"
} catch (e: PackageManager.NameNotFoundException) {
Log.e(TAG, "getAppName error", e)
""
} catch (e: Resources.NotFoundException) {
Log.e(TAG, "getAppName error", e)
""
}
}

安卓常用工具方法
https://iwesley.top/article/bf556c88/
作者
Wesley
发布于
2024年11月17日
许可协议