Androd开发之通过ComponentName和setComponent以及scheme打开外部应用 | 打开任意页面 | 打开指定页面
咱們老套路先看下效果圖:
如果公司給你這個需求,讓你們公司APP打開第三方APP進行別的操作,類似微信支付,你的APP打開微信的支付頁面這個需求。咱們就可以用今天的ComponentName來實現這個需求。
方法一
步驟:
1.被打開的APP(高斯模糊APP)被打開的頁面需要在AndroidManifest清單文件中配置允許外部應用調用,如下
<activityandroid:name=".ShowImageActivity"android:exported="true" />上面ShowImageActivity就是被打開的頁面需要配置android:exported="true"
2.幾行代碼調用即可
//第一個參數是被打開APP的包名,第二個參數是ShowImageActivity這個頁面所在的全路徑 ComponentName cName = new ComponentName("phonemsg.yhsh.cn.gaosimohu", "phonemsg.yhsh.cn.gaosimohu.ShowImageActivity");Intent intent = new Intent();intent.setComponent(cName);startActivity(intent);獲取頁面所在的全路徑的方法:
咱們看下完整代碼:
tvTittle.setOnClickListener(new View.OnClickListener() {@TargetApi(Build.VERSION_CODES.Q)@Overridepublic void onClick(View view) {//獲取默認桌面的activityIntent intent2 = new Intent(Intent.ACTION_MAIN);intent2.addCategory(Intent.CATEGORY_HOME);final ResolveInfo res = getPackageManager().resolveActivity(intent2, 0);if (res.activityInfo == null) {Log.d("打印", "resolveActivity--->activityInfo null");// should not happen. A home is always installed, isn't it?} else if (res.activityInfo.packageName.equals("android")) {// No default selectedLog.d("打印", "resolveActivity--->無默認設置");} else {// res.activityInfo.packageName and res.activityInfo.name gives// you the default appLog.d("默認桌面為:", res.activityInfo.packageName + "="+ res.activityInfo.name);}ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);ComponentName cn = am.getRunningTasks(1).get(0).topActivity;Log.d("包名", "pkg:" + cn.getPackageName());//包名Log.d("打開activity的全路徑", "cls:" + cn.getClassName());//包名加類名//跳轉到android默認桌面,第一個參數是包名,第二個是打開的頁面所的全路徑 // ComponentName cName = new ComponentName(res.activityInfo.packageName, res.activityInfo.name); // ComponentName cName = new ComponentName("com.sec.android.app.launcher", "com.android.launcher2.Launcher");ComponentName cName = new ComponentName("phonemsg.yhsh.cn.gaosimohu", "phonemsg.yhsh.cn.gaosimohu.ShowImageActivity");Intent intent = new Intent(); // Intent intent = new Intent(Intent.ACTION_MAIN); // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setComponent(cName);startActivity(intent);}});方法二
實際上還有方法可以打開指定APP
咱們可以通過scheme協議:
打開代碼:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("yhsh://cn")));然后在被打開的activity配置scheme協議,下面配置的host和scheme要與上面的調用代碼一致
<activity android:name=".WaitForStartActivity"><intent-filter><action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT" /><dataandroid:host="cn"android:scheme="yhsh" /></intent-filter></activity>好了,可以拿去玩了
方法三
在要打開的activity頁面中配置action如下圖:
下面的action參數必須和最下面調用這個頁面的參數action一樣才可以正常啟動
注意:必須設置默認category 這個:?<category android:name="android.intent.category.DEFAULT"/>否則會報錯
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.reapal.fromopenapp"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".FromOpenAppActivity"><intent-filter><category android:name="android.intent.category.DEFAULT"/><action android:name="com.reapal.fromopenapp.intent.action.FromOpenAppActivity" /></intent-filter></activity></application></manifest>如何調用呢?
intent里面的參數必須和上面xml里面配置的action一模一樣 startActivity(new Intent("com.reapal.fromopenapp.intent.action.FromOpenAppActivity"));就是這么簡單!
方法四:包名打開APP
/*** 打開機鋒世界APP*/private void openJfApk() {Intent intent = getPackageManager().getLaunchIntentForPackage("cn.net.gfan.world");if (intent != null) {intent.putExtra("type", "110");intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(intent);}}方法五:通過共享id?sharedUserId
在要被打開的activity的APP中的xml清單文件中配置如下id屬性
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.reapal.fromopenapp" sharedUserId="com.xiayiye5.yhsh">依舊通過下面方法打開外部App的activity頁面
//第一個參數是被打開APP的包名,第二個參數是ShowImageActivity這個頁面所在的全路徑 ComponentName cName = new ComponentName("phonemsg.yhsh.cn.gaosimohu", "phonemsg.yhsh.cn.gaosimohu.ShowImageActivity");Intent intent = new Intent();intent.setComponent(cName);startActivity(intent);總結
以上是生活随笔為你收集整理的Androd开发之通过ComponentName和setComponent以及scheme打开外部应用 | 打开任意页面 | 打开指定页面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: XML中的模式Schema是什么意思
- 下一篇: codepen.io是什么工具