android一個app打開另一個app的指定頁面
阿新 • • 發佈:2019-02-21
category 目標 andro lean 一個 uri tco 安裝 是否
一個app打開另一個app的指定頁面方法 有以下幾種
1、通過包名、類名
2、通過intent的 action
3、通過Url
方案1、
ComponentName componentName = new ComponentName("com.example.bi", "com.example.bi.SplashActivity");//這裏是 包名 以及 頁面類的全稱
Intent intent = new Intent();
intent.setComponent(componentName);
intent.putExtra("type", "110");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
1.在Activity上下文之外啟動Activity需要給Intent設置FLAG_ACTIVITY_NEW_TASK標誌,不然會報異常。
2.加了該標誌,如果在同一個應用中進行Activity跳轉,不會創建新的Task,只有在不同的應用中跳轉才會創建新的Task
方案2、
在目標Activity的配置文件中添加具體的action
<!--ACTION啟動配置--> <intent-filter> <action android:name="com.example.bi" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>
Intent intent = new Intent();
intent.setAction("com.example.bi");
intent.putExtra("type", "110");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
方案3、
<!--URL啟動啟動配置-->
<intent-filter>
<data
android:host="com.example.bi"
android:path="/cyn"
android:scheme="csd" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
Intent intent = new Intent();
intent.setData(Uri.parse("csd://com.example.bi/cyn?type=110"));
intent.putExtra("", "");//這裏Intent當然也可傳遞參數,但是一般情況下都會放到上面的URL中進行傳遞
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
判斷要打開的app是否安裝:
public static boolean isApkInstalled(Context context, String packageName) {
if (TextUtils.isEmpty(packageName)) {
return false;
}
try {
ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
} catch (NameNotFoundException e) {
e.printStackTrace();
return false;
}
}
android一個app打開另一個app的指定頁面