Android 拉起另一个APP

https://blog.csdn.net/canot/article/details/50571305

https://blog.csdn.net/code_dream_wq/article/details/82801452

URL: 这种方式同样适用于HTML中的a标签链接拉起B应用。 B应用清单文件需要配置: 在启动页(SplashActivity)清单文件增加如下配置:注意:不要在原有的intent-filter中增加代码,而是在原有intent-filter下方再增加一个intent-filter。 

<!--URL启动启动配置-->
            <intent-filter>
                <data
                    android:host="pull.csd.demo"
                    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>
 
---------------------
 
注意:这里scheme为必填,host、path为选填。选填内容可以不填,但是一旦填上了就必须全部完全匹配才会成功拉起。

A应用编码方式:

Intent intent = new Intent();
                intent.setData(Uri.parse("csd://pull.csd.demo/cyn?type=110"));
                intent.putExtra("", "");//这里Intent当然也可传递参数,但是一般情况下都会放到上面的URL中进行传递
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
 
---------------------
 B应用启动页拉起后可以获取到Uri,你可以选择先存储到SharedPreferences中,在主页或者广播接收者中取出来,然后再对URI进行解析;或者在启动页立刻将Uri解析成bean对象,放到全局的Application中,在主页或者广播接收者中直接使用。

以下是B应用的启动页(SplashActivity)中简单的解析示例:

Intent intent = getIntent();
        Uri uri = intent.getData();
        if (uri != null) {
            String scheme = uri.getScheme();//csd
            String uriHost = uri.getHost();//pull.csd.demo
            String uriPath = uri.getPath();///cyn
            String type = uri.getQueryParameter("type");//110
        }
 
---------------------
如果存储到SharedPreferences中那么一定是把Uri转换成String类型了,下面是你可能会用到的把String转成Uri类型的方法,贴出来省得大家出去百度了哈: Uri uri = Uri.parse(url); 

切记:A应用拉起B应用的编码千万不要忘记添加:intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 这种启动模式如果不添加你会发现有时候返回顺序是混乱的: 举个例子: 如果你在浏览B应用的M页面(从B应用主页面进入的),点击HOME键退出,你打开了A应用,从A应用拉起了B应用的N页面。 此时我们需要的合理友好的的返回顺序应该是:B应用的N页面-B应用的M页面-B应用主页-A应用-桌面,但是你会发现你返回的顺序是:B应用的N页面-A应用-B应用的M页面-B应用主页-桌面。 
--------------------- 
作者:code_dream_wq 
来源:CSDN 
原文:https://blog.csdn.net/code_dream_wq/article/details/82801452 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/qq_31390999/article/details/90813784