html页面打开app详解

2,918 阅读1分钟

一、通过Html页面打开App

1、首先需要在AndroidMainifest.xml中,对你要启动的activity(基本都是MainActivity)进行如下设置:

<activity
     android:name=".MainActivity"
     android:launchMode="singleTask"
     android:label="@string/app_name" >
     <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
            
     <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:host="my.com" 
            android:scheme="m" />
     </intent-filter>
</activity>

2、然后编写一个简单的html页面

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>

    <body>
        <a href="m://my.com/">打开app</a>
        //<a href="m://my.com/?arg0=0&arg1=1">打开app</a>
    </body>
</html>

2.1、浏览器打开如下html页,即可启动App。

<a href="myscheme://">打开app</a>

2.2、浏览器打开App时,如何获取网页带过来的数据。

<a href="m://my.com/?arg0=0&arg1=1">打开app</a>

2.3、使用“手机浏览器”或者“webview”的方式打开这个html网页,对应获取数据的方法

(1)浏览器打开这个网页的

Uri uri = getIntent().getData();  
String test1 = uri.getQueryParameter("arg0"); 
String test2 = uri.getQueryParameter("arg1");

(2)webview访问该网页的

webView.setWebViewClient(new WebViewClient(){
  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
          Uri uri = Uri.parse(url);
          if(uri.getScheme().equals("m") && uri.getHost().equals("my.com")){
              String arg0 = uri.getQueryParameter("arg0");
              String arg1 = uri.getQueryParameter("arg1");
          }else{
              view.loadUrl(url);
          }
         return true;
  }
});

二、URL scheme的格式

客户端自定义的 URL 作为从一个应用调用另一个的基础,遵循 RFC 1808 (Relative Uniform Resource Locators) 标准。这跟我们常见的网页内容 URL 格式一样。

一个普通的 URL 分为几个部分,scheme、host、relativePath、query。

比如:http://www.baidu.com/s?rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709, 这个URL中,scheme 为 http,host 为www.baidu.com,relativePath 为 /s,query 为 rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709。

一个应用中使用URL的例子(该URL会调起车辆详情页):uumobile://mobile/carDetail?car_id=123456,其中 scheme 为 uumobile,host 为mobile,relativePath 为 /carDetail,query 为 car_id=123456。