一般我们用QQ、微信打开用网易新闻,今日头条等应用分享过来的链接时,在QQ、微信内置浏览器的顶部有 “立即打开” 等按钮,点击可以只有Android手机上有安装网易、今日头条的应用,则会启动对应的应用展示相关界面。实现方式就是通过自定义Uri。
对Uri不了解可参考:http://blog.csdn.net/harvic880925/article/details/44679239
Uri形式如下:[scheme:][//host:port][path][?query][#fragment]
1.在自身应用 AndroidManifest.xml对应的Activity加入
 <intent-filter>
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.BROWSABLE" />
      <data android:scheme="openapp" android:host="post"/>
 </intent-filter>
2.在Html相关位置:
 <a href='waychel://post/123456'>打开应用</a>
这样当其他应用浏览器打开网页时,点击上述加入的链接,即会打开应用到相应的Activity。
另外:如果我们的应用中内置浏览器,如果支持这些自定义协议呢?
     处理方式,是在WebView的 shouldOverrideUrlLoading() 方法加入
 1
2
3
4
5
6
7
8
9
10
11
12
13if(url != null && (url.startsWith("http:") || url.startsWith("https:"))){
     return false;
 }
 try {
     Intent intent = new Intent();
     intent.setAction(Intent.ACTION_VIEW);
     intent.setData(Uri.parse(url));
     intent.addCategory(Intent.CATEGORY_DEFAULT);
     context.startActivity(intent);
     return true;
 }catch (Exception e){
     LogUtils.e(e.getMessage(),e);
 }
弄这块的时候,研究了下网易新闻加载的方式,发现网易网页端做了一下特殊处理,防止一些浏览器不支持或本地没有安装网易新闻的应用,从而会导致跳转到一个错误的页面。网易新闻的处理方式,大概是在当前页面弄了个空白的iframe,点击打开应用的时候,先更新下iframe再跳转至下载页面。
附上代码:
   <html>
 <head></head>
 <body>
    <a onclick="isOpenApp()">打开应用</a>
    <iframe id="zeroId" style="display: none !important;" src="">
   <script>
       function isOpenApp(){
         //加载iframe
         document.getElementById('zeroId').src='waychel://post/123456';
         //跳转到新的页面
         window.location.href='http://waychel.com';
   }
 </script>
 </body>
 </html>