Android APPLink的使用

作者: 付亮 | 来源:发表于2017-05-27 16:43 被阅读3721次

一.什么是APPLink
通过link这个词可以看出这是一种链接。这种链接用于APP,通过指定的<intent-filter>来实现跳转。谷歌从Android M开始支持APPLink,并且推动DeepLink的发展,本文会介绍link的使用方法,暂时不涉及到DeepLink的使用。
二.使用APPLink的场景
1.通过手机短信中的链接启动APP
2.通过推送过来的消息启动APP,并跳转的相关的页面。
3.与H5交互的时候,通过JS中包含的Link参数进行相关的操作。
三.APPLink的配置方法

//在AndroidMainFast文件中,对应的Activity中添加
<intent-filter android:autoVerify="true">            
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="com.fuliang.linkdemo"
android:scheme="fuliang" />
</intent-filter>

其中host与scheme可以自己来定义,上面的配置中我们拼接出来的URI = fuliang://com.fuliang.linkdemo
通过另外一个APP我们可以测试一下,在App2中跳转到APP1中的页面。

//其他的代码太简单就不贴了,XML文件中写一个button就可以了
Button button = (Button) findViewById(R.id.button);
button .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String uri = "fuliang://com.fuliang.linkdemo"
                startActivity(new Intnet(Intent.ACTION_VIEW, Uri.parse(uri)));
            }
        });

这样我们点击按钮的时候会根据申明了<action android:name="android.intent.action.VIEW" />属性的Activity去匹配host与scheme,一旦匹配成功,就会启动对应的Activity。
上面三个使用方式的用法:
1.在手机短信中我们看到的应该是一个超链接文本,点击后去匹配我们设置的host与scheme,在浏览器中直接输入link是没有作用的。
2.在推送中我们以极光推送为例
我们在附加字段中添加key和value


JPush.png
value = fuliang://com.fuliang.linkdemo?params={"name":"fuliang"}

推送出去之后我们在自定义的BroadcastReceiver中监听用户的操作事件

if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
    Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
    startApp(context, bundle);
}
private void startApp(Context context,Bundle bundle){
        String data = bundle.getString(JPushInterface.EXTRA_EXTRA);
        String uri = "";
        Intent detailIntent;
        try {
            JSONObject jsonObject = new JSONObject(data);
            uri = jsonObject.getString("url");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Intent mainIntent = new Intent(context,MainActivity.class);
        mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        detailIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
        detailIntent.putExtra("params", StringUtils.toString(Uri.parse(uri).getQueryParameter("params")));
        Intent[] intents = {mainIntent, detailIntent};
        context.startActivities(intents);
}

在推送过来的消息中,我们传的url参数在Bundle中。我们定义的规则是将推送的附加参数作为转成json字符串进行传递。接收到的json解析成为对应的uri然后我们先跳转到APP的首页在跳转到对应的ACTION_VIEW。在这里我们默认APP是存活的,在实际应用中我们应该先去判断APP是否存活,如果存活则使用这样的方式,如果没有存活则先启动APP,再解析相应的参数作出处理。
3.通过H5交互使用link
在webview中的设置:

webSettings.setJavaScriptEnabled(true);   // 支持Javascript
webView.addJavascriptInterface(this, "fuliang");  //对应接口的名字
//JavascriptInterface代表由JS来调用的原生方法,这里的方法名字一定要和H5里调用的方法一致
//传递的参数我们还是定义成一个Json字符串
@JavascriptInterface       
public void  getJSMethod(final String jsonStr){
    JSONObject jsonObject = new JSONObject(jsonStr);
    route = jsonObject.getString("address");
    startActivity(new Intnet(Intent.ACTION_VIEW, Uri.parse(route)));
}

在H5中对应的写法为:

function android() {
window.location.href = "haili://com.haili.main"; /***打开安卓app的安卓协议***/
t = Date.now();
setTimeout(function() {
    if(t - Date.now() < 1000) {
          location.href = '[图片上传中。。。(1)]http://a.app.qq.com/o/simple.jsp?pkgname=com.haili.finance';
    }
},1000);
return false;
};

以上就是常见的使用link的相关方法。

相关文章

  • Android APPLink的使用

    一.什么是APPLink通过link这个词可以看出这是一种链接。这种链接用于APP,通过指定的 来实现跳转。谷歌从...

  • Universal link 和 Applink Unity中

    官网地址Applink:'https://developer.android.com/training/app-l...

  • 测试deeplink 和applink

    链接地址: deeplink applink-http applink-https

  • 工具 | Android Studio 2.3镜像更新,极速下载

    Android Studio 2.3发布了,更新了很多不错的功能,比如WebP的支持,AppLink助手等,,因为...

  • Android DeepLink vs AppLink

    一、概念 1. 介绍 DeepLink与AppLink,本质上都是基于Intent框架,使App能够识别并处理来自...

  • AppLink

  • AppLink

    https://blog.csdn.net/u013651405/article/details/10148027...

  • AppLink

    简介 大家好!我是Tony,一个热爱技术,希望运用技术改变生活的的追梦男孩。闲话不多说,之前我总结过iOS的Uni...

  • Android DEPPLINK及APPLink原理简析

    APP开发中经常会有这种需求:在浏览器或者短信中唤起APP,如果安装了就唤起,否则引导下载。对于Android而言...

  • W

    http://www.guguaixia.com/eagle-frontap/jiabei/applink/FM0...

网友评论

本文标题:Android APPLink的使用

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