webview/h5跳转到app的指定activity

来源:互联网 发布:fm2016中文版for mac 编辑:程序博客网 时间:2024/06/04 23:23

开发时有时会碰到这样的需求,分享到第三方的h5页面,在点击操作的时候需要跳回APP的指定页面,这时只需要在该activity下面配置一下相应的scheme host等信息就可以了

App中配置:

<activity    android:name=".xxActivity"    android:launchMode="singleTask"    android:screenOrientation="portrait"    android:configChanges="keyboardHidden|orientation"    android:windowSoftInputMode="adjustPan">    <intent-filter>    <action android:name="android.intent.action.VIEW" />    <category android:name="android.intent.category.BROWSABLE" />    <category android:name="android.intent.category.DEFAULT"/>    <data        android:host="my"        android:scheme="app"        android:path="/my"        /></intent-filter>    </activity>
h5或webview中配置:
<!DOCTYPE html><html><head>    <meta charset="utf-8">    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />    <title></title>     <script type="text/javascript" charset="utf-8">                function clickToApp(){            window.location.href="app://myapp/my?type=1";         }        }                  </script>     </head><body>              <button onclick="clickToApp()">跳转到app指定页面</button>            </body></html>

补充:
1.可以通过
Uri mData = this.getIntent().getData();if (mData != null) {   String mType = mData.getQueryParameter("type");}
获取h5或者webview传递过来的值

2.如果在webview中没法正常跳转,可以配置webview如下:

// 选择跳转方式,true在本应用跳转,false通过浏览器跳转mWebView.setWebViewClient(new WebViewClient() {    @Override    public void onPageStarted(WebView view, String url, Bitmap favicon) {      }    @Override    public boolean shouldOverrideUrlLoading(WebView view, String httpurl) {        if (httpurl.startsWith("app:")) {            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(httpurl));            startActivity(intent);        }        return false;    }});