美文网首页安卓开发博客
Android APP打开另一个APP

Android APP打开另一个APP

作者: 颤抖的闪电 | 来源:发表于2019-08-23 17:28 被阅读0次

A拉起B可实现的几种方法

(1)包名,特定Activity名拉起

                Intent intent = new Intent(Intent.ACTION_MAIN);
                /**知道要跳转应用的包命与目标Activity*/
                ComponentName componentName = new ComponentName("com.example.demo", "com.example.mobile.module.starter.StarterActivity2");
                intent.setComponent(componentName);
                intent.putExtra("username", "username");//这里Intent传值
                intent.putExtra("password", "password");//这里Intent传值
                startActivity(intent);

B应用需要在manifest文件对应Activity添加

android:exported="true"

(2)Action 拉起(这里就是进去启动页)

Intent intent = new Intent();
                intent.setAction("com.example.demo.starter.info2");
                intent.addCategory("android.intent.category.DEFAULT");
                intent.setPackage("com.example.demo");
//                intent.putExtra("username", mInstance.encrypt("username"));//这里Intent传值
//                intent.putExtra("password", mInstance.encrypt("password"));//这里Intent传值
                startActivity(intent);

B应用需要在manifest文件对应Activity配置action、category

<activity
            android:name=".module.starter.StarterActivity2"
            android:exported="true"
            android:launchMode="singleTask"
            android:theme="@style/TranslucentTheme">
            <intent-filter>
                <action android:name="com.example.demo.starter.info2" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

(3)url拉起(scheme)

webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                try {
                    // 处理自定义scheme协议
//                    if (url.startsWith("http") || url.startsWith("https") || url.startsWith("ftp")) {
                    if (url.contains("mmp.csair.com/manualDetails")) {
                        try {
                            // 以下固定写法
                            final Intent intent = new Intent("com.example.demo.starter.info3",
                                    Uri.parse(url));
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                    | Intent.FLAG_ACTIVITY_SINGLE_TOP);

                            //传参方法二 采用传参方法二,因为加密之后会出现很多特殊符号,不适用传参方法一
//                            intent.putExtra("username", mInstance.encrypt("username"));//这里Intent传值
//                            intent.putExtra("password", mInstance.encrypt("password"));//这里Intent传值

                            startActivity(intent);
                        } catch (Exception e) {
                            // 防止没有安装的情况
                            e.printStackTrace();
                            Toast.makeText(activity, "您所打开的第三方App未安装!", Toast.LENGTH_LONG).show();
                        }
                        return true;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                /* mWebView.showLog("test Log"); */
            }
        });

B应用需要在manifest文件对应Activity配置action、category、data

<activity
            android:name=".module.starter.StarterActivity3"
            android:exported="true"
            android:launchMode="singleTask"
            android:theme="@style/TranslucentTheme">
            <!--需要添加下面的intent-filter配置-->
            <intent-filter>
                <action android:name="com.example.demo.starter.info3" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="host.deom.com"
                    android:scheme="http" />
                <data
                    android:host="host.deom.com"
                    android:scheme="https" />
                <data
                    android:host="host.deom.com"
                    android:scheme="ftp" />
            </intent-filter>
        </activity>

后言感谢:
Android APP打开另一个APP完整逻辑实现
Android WebView中跳转第三方App

相关文章

网友评论

    本文标题:Android APP打开另一个APP

    本文链接:https://www.haomeiwen.com/subject/sqstectx.html