context?.packageManager?.getInstalledPackages(0)?.forEach {
it?.let { p ->
if (p.packageName == GOOGLE_MAP_PACKAGE_NAME) {
googleMapIcon.setImageDrawable(p.applicationInfo.loadIcon(context?.packageManager))
googleMapText.text =
context?.packageManager?.getApplicationLabel(p.applicationInfo)
.toString()
}
if (p.packageName == WAZE_PACKAGE_NAME) {
weizhiMapIcon.setImageDrawable(p.applicationInfo.loadIcon(context?.packageManager))
weizhiText.text =
context?.packageManager?.getApplicationLabel(p.applicationInfo)
.toString()
}
}
}
使用这种方法来判断多个一个列表app是否有安装过,应用会几率出现DeadSystemException问题,不管怎么使用最后还是会偶尔出现报错和crash错误。?
Fatal Exception: java.lang.RuntimeException: android.os.DeadSystemException
at android.app.ApplicationPackageManager.getInstalledPackagesAsUser(ApplicationPackageManager.java:1031)
at android.app.ApplicationPackageManager.getInstalledPackages(ApplicationPackageManager.java:1016)
at com.xxx.xxx.app_common.utils.NavigateSelectUtils$NavigateDialog.onCreateView(NavigateSelectUtils.java:458)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.DialogFragment.performCreateView(DialogFragment.java:489)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:250)
at android.app.ActivityThread.main(ActivityThread.java:7844)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
报错的地点很明显是运行时异常
?
?解决方案:
try {
context?.packageManager?.getApplicationInfo(
GOOGLE_MAP_PACKAGE_NAME,
PackageManager.GET_META_DATA
)?.run {
googleMapIcon.setImageDrawable(this.loadIcon(context?.packageManager))
googleMapText.text =
context?.packageManager?.getApplicationLabel(this)
.toString()
}
context?.packageManager?.getApplicationInfo(
WAZE_PACKAGE_NAME,
PackageManager.GET_META_DATA
)?.run {
weizhiMapIcon.setImageDrawable(this.loadIcon(context?.packageManager))
weizhiText.text =
context?.packageManager?.getApplicationLabel(this)
.toString()
}
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
}
|