转载地址:http://hunankeda110.iteye.com/blog/1960444
* 启动另外一个程序*
第一种方式知道包名和类名:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
設置包名兩種方式
1、 PackageManager pm=this.getPackageManager();
intent= pm.getLaunchIntentForPackage("pad.android.marketing.gps");
intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASKIntent.FLAG_ACTIVITY_RESET_TASK_IF_NEED ED);
2、ComponentName cm = new ComponentName("pad.android.marketing.gps", "pad.android.marketing.gps.MainActivity");
intent.setComponent(cm);
startActivity(intent);
第二种启动不知道类名的,包名需要从设备查找。
Intent intent1 = new Intent();
intent1.setAction(Intent.ACTION_MAIN);
intent1.addCategory(Intent.CATEGORY_LAUNCHER);
//包管理器
PackageManager pm = context.getPackageManager();
//已经安装的app信息集合
List<ApplicationInfo> appList = pm.getInstalledApplications(0);
for (int i = 0; i < appList.size(); i++) {
Log.e("TAG", "appinfo: " + appList.get(i));
}
//包信息实例化
PackageInfo packageInfo = null;
try {
//指定包的信息
packageInfo = pm.getPackageInfo("pad.android.marketing.gps", 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
//指定app进入界面activity。
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setPackage(packageInfo.packageName);
//查找指定包中activity信息
List<ResolveInfo> infoList = pm.queryIntentActivities(intent, 0);
ResolveInfo ri = infoList.iterator().next();
if (ri != null) {
//启动activity的名字
String classname = ri.activityInfo.name;
Log.e("TAG", "class " +classname );
}
ComponentName cn = new ComponentName("pad.android.marketing.gps", className);
intent1.setComponent(cn);
startActivity(intent1);