Html中启动本地app

2019-08-21  本文已影响0人  lotusve_w
  1. 前言:

html中启动本地app

  1. html:

<a href="appscheme://apphost/openwith?name=zhang3&age=30">Start App</a>

格式: [scheme]://[host]/[path]?[query]
html代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Start App</title>
    </head>
    <body>
        <a href="appscheme://apphost/openwith?name=zhang3&age=30">Start App</a>
    </body>
</html>
  1. android 代码

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="appscheme" android:host="apphost" android:pathPrefix="/openwith"/>  
</intent-filter>

获取参数, 启动的Activity中:

try {
    Uri uri = getIntent().getData();
    String query = uri.getQuery();
    String name = uri.getQueryParameter("name");
    String age = uri.getQueryParameter("age");
    Log.d("appscheme-", "name: " + name + ", age: " + age + ", query: " + query);
} catch (Exception e) {
    Log.e("appscheme-", e.getMessage(), e);
}
上一篇下一篇

猜你喜欢

热点阅读