【发布时间】:2019-06-04 18:07:19
【问题描述】:
到目前为止我的代码:
val listofApps:List<ApplicationInfo> = packageManager.getInstalledApplications(PackageManager.GET_META_DATA)
var test: Array<ActivityInfo>
try{
test = packageManager.getPackageInfo(listofApps.get(2).packageName, PackageManager.GET_ACTIVITIES).activities
var s: String = "App " + listofApps.get(2).packageName + " has activities: "
for (activityInfo in test) {
s = s + activityInfo.name + " "
}
Toast.makeText(this, s, Toast.LENGTH_SHORT).show()
}catch (e:Exception){
Toast.makeText(this, e.message, Toast.LENGTH_SHORT).show()
}
我得到一个异常,因为 test 为空。如果我使用 context.packageName 而不是 listOfApps.get(2) 它会按预期工作。我没有在清单中包含外部应用程序,但由于我将使用手机上安装的所有应用程序执行此操作,因此有时可能会有新应用程序我无法声明它们,因此我需要一个更动态解决方案。
如何修复代码以便从应用中获取所有活动?
【问题讨论】:
标签: android android-studio android-activity