如果你知道目標應用的包名和想打開的界面類名,那就很方便。
//這些代碼是啓動另外的一個應用程序的主Activity,當然也可以啓動任意一個Activity
ComponentName componetName = new ComponentName(
//這個是另外一個應用程序的包名
"com.poynt.weibo",
//這個參數是要啓動的Activity
"com.poynt.weibo.ui.IndexActivity");
try {
Intent intent = new Intent();
intent.setComponent(componetName);
startActivity(intent);
} catch (Exception e) {
// Toast.makeText(getApplicationContext(), "可以在這裏提示用戶沒有找到應用程序,或者是做其他的操作!", 0).show();
如果你不知道目標應用的界面類名,可以使用下面的方法,啓動目標應用。
/** 獲取PackageManager對象 */
PackageManager packageManager = getPackageManager();
PackageInfo pi = null;
try {
pi = packageManager.getPackageInfo("com.welove520.welove", 0);
} catch (PackageManager.NameNotFoundException e) {
}
// 設置<intent-filter>標籤內需要滿足的條件
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(pi.packageName);
// 通過queryIntentActivities獲取ResolveInfo對象
List<ResolveInfo> apps = packageManager.queryIntentActivities(resolveIntent, 0);
ResolveInfo ri = apps.iterator().next();
if (ri != null ) {
String className = ri.activityInfo.name;
Intent intent = new Intent();
ComponentName cn = new ComponentName("com.welove520.welove", className);
intent.setComponent(cn);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Context context = getApplicationContext();
context.startActivity(intent);
}