JsBridge为BridgeWebView添加自定义跳转协议

JsBridge: https://github.com/lzyzsd/JsBridge 自带的BridgeWebView并未添加yy://之外的协议,需要自行实现,如通用协议如电话、短信、邮件以及自定义协议如打开微信等。

1
2
3
4
<a href="tel:10086">电话</a>
<a href="sms:10086">短信</a>
<a href="mailto:yezhou@yezhou.org">邮件</a>
<a href="weixin://">打开微信</a>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
private WebViewClient getWebViewClient() {
return new WebViewClient() {
BridgeWebViewClient mBridgeWebViewClient = new BridgeWebViewClient(mBridgeWebView);

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
NLog.i(Constants.TAG, "url: " + request.getUrl().toString());
String url = request.getUrl().toString();
if (shouldOverrideUrlLoading(url)) {
return true;
} else {
return mBridgeWebViewClient.shouldOverrideUrlLoading(view, request);
}
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
NLog.i(Constants.TAG, "url: " + url);
if (shouldOverrideUrlLoading(url)) {
return true;
} else {
return mBridgeWebViewClient.shouldOverrideUrlLoading(view, url);
}
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
mBridgeWebViewClient.onPageStarted(view, url, favicon);
}

@Override
public void onPageFinished(WebView view, String url) {
//super.onPageFinished(view, url);
mBridgeWebViewClient.onPageFinished(view, url);
}

@TargetApi(Build.VERSION_CODES.M)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
//yy://__QUEUE_MESSAGE__/
LogUtils.i(TAG, "onReceivedError:" + error.getDescription() + " code:" + error.getErrorCode() + ", url: " + request.getUrl().toString());
}

private boolean shouldOverrideUrlLoading(String url) {
if (url.startsWith(WebView.SCHEME_TEL) || url.startsWith(SCHEME_SMS) || url.startsWith(WebView.SCHEME_MAILTO)) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
LianLianServiceActivity.this.startActivity(intent);
return true;
} else if (url.startsWith("weixin:")) {
//跳转微信
Intent launch = getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
if (launch == null) {
Toast.makeText(LianLianServiceActivity.this, "Wechat not installed", Toast.LENGTH_SHORT).show();
return true;
}
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(launch.getComponent());
startActivity(intent);
return true;
}
return false;
}
};
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :