How to launch applications from another application?

我们两清 提交于 2019-12-13 01:15:54

问题


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:

  1. 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

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!