webview/h5跳转到app的指定activity

开发时有时会碰到这样的需求,分享到第三方的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 οnclick="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;
    }
});

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

s_nshine

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值