浏览器通过Scheme协议启动APP中的页面

来源:互联网 发布:担保交易网站源码 编辑:程序博客网 时间:2024/05/22 00:20

    在APP开发过程中,通过外部浏览器调起APP页面的场景也很普遍使用。下面就介绍一下通过外部H5页面唤起APP中页面的通用方法。

1.首先需要在AndroidMainifest.xml中对你要启动的那个activity进行如下设置:

       <activity            android:name=".MainActivity"            android:launchMode="singleTask">            <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:scheme="myscheme://" />            </intent-filter>        </activity>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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

<a href="myscheme://">打开APP</a>
  • 1
  • 1

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

<a href="myscheme://query?arg0=0&arg1=1">打开APP</a>
  • 1
  • 1

(1).假如你是通过浏览器打开这个网页的,那么获取数据的方式为:

Uri uri = getIntent().getData();  String test1= uri.getQueryParameter("arg0"); String test2= uri.getQueryParameter("arg1");
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

(2)如果使用webview访问该网页,获取数据的操作为:

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