我使用以下代码获取已安装应用程序的列表并启动它们。它只打开计算器或时钟等应用程序。当我尝试打开联系人或摄像头等应用程序时,它不起作用,因为它们的启动活动为空。我如何打开这样的应用程序。
在下面的代码中,[5]指向相机。
final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm
.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages)
{
s[i]=packageInfo.packageName;
i++;
Log.d(TAG, "Installed package :" + packageInfo.packageName);
Log.d(TAG,"Launch Activity :"+ pm.getLaunchIntentForPackage(packageInfo.packageName));
}
}
Intent mIntent = getPackageManager().getLaunchIntentForPackage(s[5]);
try {
startActivity(mIntent);
}
catch (Exception err) {
Toast t = Toast.makeText(getApplicationContext(),
"Not Found", Toast.LENGTH_SHORT);
t.show();
}
}
}
答案 0 :(得分:1)
我终于能够解决问题。以下代码启动了所有应用程序。
PackageManager pm = this.getPackageManager();
setContentView(R.layout.main);
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> appList = pm.queryIntentActivities(mainIntent, 0);
Collections.sort(appList, new ResolveInfo.DisplayNameComparator(pm));
TextView c=(TextView)findViewById(R.id.textView1);
for(int i1=0; i1<appList.size(); i1++){
c.setText(c.getText() + "\n" +
"number: " + i1 + "\n" +
"Name: " + appList.get(i1).loadLabel(pm) + "\n"
);
}
Intent i11 = new Intent();
i11.setAction(Intent.ACTION_MAIN);
i11.addCategory(Intent.CATEGORY_LAUNCHER);
i11.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i11.setComponent(new ComponentName(appList.get (3).activityInfo.applicationInfo.packageName, appList.get(3).activityInfo.name));
startActivity(i11);