问题
I want to launch all installed applications from my application.Here i get all installed applications
List<ApplicationInfo> applicationInfoList = packageManager.getInstalledApplications(PackageManager.GET_ACTIVITIES | PackageManager.GET_META_DATA);
if(applicationInfoList != null && !applicationInfoList.isEmpty()){
Collections.sort(applicationInfoList, new ApplicationInfo.DisplayNameComparator(
packageManager));
for (ApplicationInfo applicationInfo : applicationInfoList) {
Intent intent = packageManager.getLaunchIntentForPackage(applicationInfo.packageName);
if(intent != null){
ComponentName componentName = intent.getComponent();
//add componenet to a list
}
}
}
But i can't launch some applications like Contacts and Phone.The class name is 'ResolverActivity' for these apps.How ca i launch these apps from my applications?
Thanks in Advance
回答1:
This is because Contacts and Phone are the same application, as are Maps and Latitude. They happen to have multiple launchable activities.
So, you have two choices:
Stick with your statement that you want to "launch all installed applications", in which case your existing code is correct (the user will choose whether to show Contacts or Phone), or
Do what a home screen launcher does, which is "launch all launchable activities", in which case you are going about it wrong
For the latter, use queryIntentActivities()
for a MAIN
/LAUNCHER
Intent
, and use the results to build your list. Here is a sample application that demonstrates this.
回答2:
to launch an application you can try this :
// start the app by invoking its launch intent
Intent i = getPackageManager().getLaunchIntentForPackage(applicationInfo.packageName);
try {
if (i != null) {
startActivity(i);
} else {
i = new Intent(applicationInfo.packageName);
startActivity(i);
}
} catch (ActivityNotFoundException err) {
Toast.makeText(ListInstalledApps.this, "Error launching app", Toast.LENGTH_SHORT).show();
}
refer this : tutorial1 , tutorial2 , thread stackoverflow
来源:https://stackoverflow.com/questions/16104942/how-to-launch-applications-from-another-application