Android中启动其它应用的几种方式
1,知道要启动的包名以及要启动的Activity:
Intent intent = new Intent();
/**下面方法的参数分别是指要启动应用的包名及对应的Activity*/
ComponentName cpn= new ComponentName("com.ldm.demo","com.ldm.demo.TextActivity");
intent .setComponent(cpn);
startActivity(intent );
2,只知道应用的包名:
PackageManager packageManager = getPackageManager();
Intent intent=new Intent();
intent =packageManager.getLaunchIntentForPackage("packageName");/**packageName指的就要启动应用的包名*/
if(intent==null){
System.out.println("APP not found!");
}
startActivity(intent);
}
}
3,知道要启动的包名以及要启动的Activity应用启动并传递数据:
- ComponentName componentName = new ComponentName( "com.ldm.demo", "com.ldm.demo.TestActivity");
- Intent intent = new Intent();
- Bundle bundle = new Bundle();
- bundle.putString("data", data);
- intent.putExtras(bundle);
- intent.setComponent(componentName);
- startActivity(intent);
- 4,未知包名的情况下:
- /**获得手机内应用的包名,返回一个List集合**/
public List<PackageInfo> getAllApps() {
List<PackageInfo> apps = new ArrayList<PackageInfo>();
PackageManager packageManager = this.getPackageManager();
/**获取手机内所有应用 */
List<PackageInfo> paklist = pManager.getInstalledPackages(0);
for (int i = 0; i < paklist.size(); i++) {
PackageInfo pak = (PackageInfo) paklist.get(i);
/**判断是否为非系统预装的应用 (大于0为系统预装应用,小于等于0为非系统应用) */
if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
apps.add(pak);
}
}
return apps;
}
获得包名后,就可以通过获得要启动的包名启动应用了:
public void launchApp() {
PackageManager packageManager = this.getPackageManager();
List<PackageInfo> packages = getAllApps();
PackageInfo pa = null;
for(int i=0;i<packages.size();i++){
pa = packages.get(i);
/**获得应用名*/
String appLabel = packageManager.getApplicationLabel(pa.applicationInfo).toString();
/**获得包名 */
String appPackage = pa.packageName;
Log.d(""+i, appLabel+" "+appPackage);
}
Intent intent = packageManager.getLaunchIntentForPackage("jp.co.johospace.jorte");
startActivity(intent);
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。