• 利用URL Scheme打开APP并传递数据


    https://blog.csdn.net/u013517637/article/details/55251421

    利用外部链接打开APP并传递一些附带信息是现在很多APP都有的功能,我在这把这部分的知识记录一下。

    1、什么是URL Scheme?

    android中的scheme是一种页面内跳转协议,是一种非常好的机制,通过自己在AndroidManifest.xml文件里面定义自己的scheme协议,可以非常方便的跳转到App的各个页面。通过scheme协议,甚至可以跳转到App的某个页面,可以通过直接输入URL进行跳转,也可以把URL写进HTML页面进行跳转。

    2、实现的大致流程

    我们手机的APP可以向操作系统注册一个URL Scheme,该scheme用于从浏览器或其他应用中启动本应用。

    3、URL Scheme的协议格式

    tlqp://my.app/openwith?roomID=123456

    scheme:tlqp 代表Scheme的协议名称(必须)

    host:my.app 代表host

    path:openwith 代表path

    query:roomID=123456 代表URL传递的值

    4、设置URL Scheme

    [java] view plain copy
     
    1. <intent-filter>  
    2.         <action android:name="android.intent.action.MAIN" />  
    3.         <category android:name="android.intent.category.LAUNCHER" />  
    4. </intent-filter>  
    5.               
    6. <intent-filter>      
    7.     <action android:name="android.intent.action.VIEW"/>      
    8.     <category android:name="android.intent.category.DEFAULT" />      
    9.     <category android:name="android.intent.category.BROWSABLE" />      
    10.     <data android:scheme="tlqp" android:host="my.app" android:pathPrefix="/openwith" />      
    11. </intent-filter>  

    在AndroidManifest.xml文件,添加以上代码(根据情况适当修改),这里需要注意的地方是,不能再上面的Intent-filter里面添加相关代码,因为那里面存在MAIN和LAUNCHER相关代码,混在一起的话,会造成App图标丢失的情况。需要新建一个intent-filter,然后把代码添加进去即可。

    5、获取URL附带的参数

    [java] view plain copy
     
    1. Uri uri = getIntent().getData();  
    2. if (uri != null) {  
    3.     // 完整的url信息  
    4.     String url = uri.toString();  
    5.     Log.e(TAG, "url: " + uri);  
    6.     // scheme部分  
    7.     String scheme = uri.getScheme();  
    8.     Log.e(TAG, "scheme: " + scheme);  
    9.     // host部分  
    10.     String host = uri.getHost();  
    11.     Log.e(TAG, "host: " + host);  
    12.     //port部分  
    13.     int port = uri.getPort();  
    14.     Log.e(TAG, "host: " + port);  
    15.     // 访问路劲  
    16.     String path = uri.getPath();  
    17.     Log.e(TAG, "path: " + path);  
    18.     List<String> pathSegments = uri.getPathSegments();  
    19.     // Query部分  
    20.     String query = uri.getQuery();  
    21.     Log.e(TAG, "query: " + query);  
    22.     //获取指定参数值  
    23.     String goodsId = uri.getQueryParameter("goodsId");  
    24.     Log.e(TAG, "goodsId: " + goodsId);  
    25. }  



    6、打开app的方式

    可以把URL写进HTML里面通过点击网页链接打开APP

    [html] view plain copy
     
    1. <html>  
    2.   
    3.     <head>  
    4.   
    5.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    6.       
    7.         <title>Insert title here</title>  
    8.   
    9.     </head>  
    10.   
    11.     <body>  
    12.   
    13.         <href="tlqp://my.app/openwith?roomID=203518">打开app</a><br/>  
    14.   
    15.     </body>  
    16.   
    17. </html>  


    原生调用方式:

    [java] view plain copy
     
    1. Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("xl://goods:8888/goodsDetail?goodsId=10011002"));  
    2. startActivity(intent);  


    后记:目前有一部分浏览器是打不开App的,例如;qq浏览器,UC浏览器这些,百度浏览器也有可能打不开,原因是这些浏览器都在你的Scheme前面加上了http://,从而造成了scheme失效。还有一点需要注意,就是上面说到的url里面的path这个参数,如果你的path包含了其他的path,那么你调用这个包含其他path的path,可能会在打开第一个path对应的页面的同时也会打开被包含path的那个页面,有点绕口么~举个栗子吧

  • 相关阅读:
    Retrofit2源码分析
    Android8.0硬件加速的重绘流程
    Android单元测试
    rand5->rand7,rand7->rand10
    快速排序的随机化版本
    快速排序
    亦或实现交换
    在最坏情况下,找到n个元素中第二小的元素需要n+lgn-2次比较
    3*n/2时间内求出最大最小值
    基数排序
  • 原文地址:https://www.cnblogs.com/chaoyuehedy/p/9004224.html
Copyright © 2020-2023  润新知