利⽤URLScheme打开APP并传递数据
利⽤外部链接打开APP并传递⼀些附带信息是现在很多APP都有的功能,我在这把这部分的知识记录⼀下。
1、什么是URL Scheme?
android中的scheme是⼀种页⾯内跳转协议,是⼀种⾮常好的机制,通过⾃⼰在l⽂件⾥⾯定义⾃⼰的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]
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>
在l⽂件,添加以上代码(根据情况适当修改),这⾥需要注意的地⽅是,不能再上⾯的Intent-filter⾥⾯添加相关代码,因为那⾥⾯存在MAIN和LAUNCHER相关代码,混在⼀起的话,会造成App图标丢失的情况。需要新建⼀个intent-filter,然后把代码添加进去即可。
5、获取URL附带的参数
1. Uri uri = getIntent().getData();
2. if (uri != null) {
3.    // 完整的url信息
4.    String url = String();
网页app5.    Log.e(TAG, "url: " + uri);
6.    // scheme部分
7.    String scheme = Scheme();
8.    Log.e(TAG, "scheme: " + scheme);
9.    // host部分
10.    String host = Host();
11.    Log.e(TAG, "host: " + host);
12.    //port部分
13.    int port = Port();
14.    Log.e(TAG, "host: " + port);
15.    // 访问路劲
16.    String path = Path();
17.    Log.e(TAG, "path: " + path);
18.    List<String> pathSegments = PathSegments();
19.    // Query部分
20.    String query = Query();
21.    Log.e(TAG, "query: " + query);
22.    //获取指定参数值
23.    String goodsId = QueryParameter("goodsId");
24.    Log.e(TAG, "goodsId: " + goodsId);
25. }
6、打开app的⽅式
可以把URL写进HTML⾥⾯通过点击⽹页链接打开APP
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.        <a href="tlqp://my.app/openwith?roomID=203518">打开app</a><br/>
14.
15.    </body>
16.
17. </html>
原⽣调⽤⽅式:
[java]
1. Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("xl://goods:8888/goodsDetail?goodsId=10011002"));
2. startActivity(intent);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。