Androd开发之通过ComponentName和setComponent以及scheme打开外部应用

咱们老套路先看下效果图:

如果公司给你这个需求,让你们公司APP打开第三方APP进行别的操作,类似微信支付,你的APP打开微信的支付页面这个需求。咱们就可以用今天的ComponentName来实现这个需求。

方法一

步骤:

1.被打开的APP(高斯模糊APP)被打开的页面需要在AndroidManifest清单文件中配置允许外部应用调用,如下

<activity
            android: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)
            @Override
            public void onClick(View view) {
                //获取默认桌面的activity
                Intent 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 selected
                    Log.d("打印", "resolveActivity--->无默认设置");
                } else {
                    // res.activityInfo.packageName and res.activityInfo.name gives
                    // you the default app
                    Log.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" />
                <data
                    android: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">

    <application
        android: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"));

就是这么简单!

发布了191 篇原创文章 · 获赞 105 · 访问量 36万+

猜你喜欢

转载自blog.csdn.net/xiayiye5/article/details/98083520