android开发中如何开启用户安装的应用程序?

如设置一个按钮,当用户点击按钮是可以启动qq,uc等用户程序
通过intent调用用户程序,需要你知道用户程序的动作,类别,数据。进行匹配。就可以调用了
这些应用程序的apk文件安装在你的模拟器上的程序,可以通过反编译工具查找到清单文件的包名和类名,如调用qq程序可以使用
1 Intent intent = new Intent();  
2 ComponentName componentName = 
3                        new ComponentName("com.tencent.qq","com.tencent.qq.SplashActivity");  
4 intent.setComponent(componentName);  
5 startActivity(intent);

 

总结:如何在android应用程序中启动其他apk程序,被启动程序退出后返回之前的程序
Android 开发有时需要在一个应用中启动另一个应用,比如Launcher加载所有的已安装的程序的列表,当点击图标时可以启动另一个应用。
一般我们知道了另一个应用的包名和MainActivity的名字之后便可以直接通过如下代码来启动:
1 Intent intent = new Intent(Intent.ACTION_MAIN);
2 intent.addCategory(Intent.CATEGORY_LAUNCHER);           
3 ComponentName cn = new ComponentName(packageName, className);           
4 intent.setComponent(cn);
5 startActivity(intent);
但是更多的时候,我们一般都不知道应用程序的启动Activity的类名,而只知道包名,我们可以通过ResolveInfo类来取得启动Acitivty的类名。
下面是实现代码:
 1  private void openApp(String packageName) {
 2     PackageInfo pi = getPackageManager().getPackageInfo(packageName, 0);
 3       
 4     Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
 5     resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
 6     resolveIntent.setPackage(pi.packageName);
 7       
 8     List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);
 9       
10     ResolveInfo ri = apps.iterator().next();
11     if (ri != null ) {
12     String packageName = ri.activityInfo.packageName;
13     String className = ri.activityInfo.name;
14       
15     Intent intent = new Intent(Intent.ACTION_MAIN);
16     intent.addCategory(Intent.CATEGORY_LAUNCHER);
17       
18     ComponentName cn = new ComponentName(packageName, className);
19       
20     intent.setComponent(cn);
21     startActivity(intent);
22     }
23   }

 

posted @ 2013-04-03 17:17  lpfuture  阅读(519)  评论(0编辑  收藏  举报