现在淘宝网站貌似升级了,安卓开发时,用webview打开淘宝网站时会自动打开掏宝app。(其他浏览器也是这样)。如何阻止打开淘宝网站时自动打开淘宝app。
下面是webview打开http://h5.m.taobao.com/awp/core/detail.htm?id=563621033487 时的日志:
02-01 18:37:16.280 24675-24792/com.sugem.myapplication D/NetworkSecurityConfig: No Network Security Config specified, using platform default
02-01 18:37:16.664 24675-24675/com.sugem.myapplication W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 24675
02-01 18:37:16.678 24675-24675/com.sugem.myapplication I/chromium: [INFO:CONSOLE(2)] "The key "viewport-fit" is not recognized and ignored.", source: http://h5.m.taobao.com/awp/core/detail.htm?id=563621033487 (2)
02-01 18:37:17.147 24675-24746/com.sugem.myapplication W/WebKit: A Parser-blocking, cross-origin script, http://g.alicdn.com/mtb/lib-smartbanner-plus-loader/smartbanner-loader.js?t=15174814, is invoked via document.write. This may be blocked by the browser if the device has poor network connectivity. See https://www.chromestatus.com/feature/5718547946799104 for more details.
02-01 18:37:17.148 24675-24675/com.sugem.myapplication I/chromium: [INFO:CONSOLE(5)] "A Parser-blocking, cross-origin script, http://g.alicdn.com/mtb/lib-smartbanner-plus-loader/smartbanner-loader.js?t=15174814, is invoked via document.write. This may be blocked by the browser if the device has poor network connectivity. See https://www.chromestatus.com/feature/5718547946799104 for more details.", source: http://h5.m.taobao.com/awp/core/detail.htm?id=563621033487 (5)
02-01 18:37:18.049 24675-24675/com.sugem.myapplication I/chromium: [INFO:CONSOLE(98)] "[object Object]", source: webpack:///./src/js/stores/AppStores.js? (98)
02-01 18:37:18.165 24675-24675/com.sugem.myapplication I/chromium: [INFO:CONSOLE(5)] "warning: This interface is deprecated, please use goldlog.sendPV instead! API: http://log.alibaba-inc.com/log/info.htm?type=2277&id=31", source: http://g.alicdn.com/alilog/??s/8.0.1/plugin/aplus_client.js,aplus_cplugin/0.2.12/toolkit.js,aplus_cplugin/0.2.12/monitor.js,s/8.0.1/aplus_wap.js,aplus_cplugin/0.2.12/aol.js,s/8.0.1/plugin/aplus_spmact.js?v=20180130174216 (5)
02-01 18:37:19.535 24675-24675/com.sugem.myapplication I/chromium: [INFO:CONSOLE(8)] "The deviceorientation event is deprecated on insecure origins, and support will be removed in the future. You should consider switching your application to a secure origin, such as HTTPS. See https://goo.gl/rStTGz for more details.", source: http://aeu.alicdn.com/js/cj/106.js (8)
02-01 18:37:19.552 24675-24675/com.sugem.myapplication D/SensorManager: +++(enableSensor)+++: type= 15,rateUs= 16666,package= com.sugem.myapplication,handle= 1600610674,maxBatchReportLatencyUs= 0
URL 语法
URL由三部分组成:资源类型、存放资源的主机域名、资源文件名。
对淘宝的自定义协议进行处理
URL的一般语法格式为:
(带方括号[]的为可选项):
protocol :// hostname[:port] / path / [;parameters][?query]#fragment
public static class CommonWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("leo", "[webViewUrl]" + url);
if (url.startsWith("tel")) {// 机构电话
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
Uri uri = Uri.parse(url);
if ("baonajia".equals(uri.getScheme())) {
if ("organization".equals(uri.getHost())) {// 课程详情跳转到机构详情
Organization organization = new Organization();
organization.setId(uri.getQueryParameter("organization_id"));
organization.setName(uri.getQueryParameter("organization_name"));
Intent intent = new Intent(view.getContext(), OrganizationDetailActivity.class);
intent.putExtra(Const.EXTRA_ORG, organization);
view.getContext().startActivity(intent);
} else if ("course".equals(uri.getHost())) {// 机构详情跳转到课程详情
new CourseTask(view, uri.getQueryParameter("course_id")).execute();
} else if ("school".equals(uri.getHost())) {// 机构详情跳转到校区列表
String orgId = uri.getQueryParameter("organization_id");
Intent intent = new Intent(view.getContext(), SchoolListActivity.class);
intent.putExtra(Const.EXTRA_ORG_ID, orgId);
view.getContext().startActivity(intent);
} else if ("album".equals(uri.getHost())) {// 机构详情跳转到相册列表
String orgId = uri.getQueryParameter("organization_id");
Intent intent = new Intent(view.getContext(), PhotoAlbumListActivity.class);
intent.putExtra(Const.EXTRA_ORG_ID, orgId);
view.getContext().startActivity(intent);
}
return true;
} else {
view.loadUrl(url);
return true;
}
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG,
String.format("errorCode: %s, description: %s, when open: %s", errorCode, description, failingUrl));
view.loadUrl(URLCenter.getError());
}
}
通过拦截url就可以了,他这个之所以会跳转手机淘宝,因为一加载这个网页他自己就发一个跳转淘吧app的url的事件,那我们拦截他就好了,下面是我写的代码,亲测有效
public class ThreeActivity extends AppCompatActivity {
private WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
myWebView = (WebView) findViewById(R.id.webview);
// Configure related browser settings
myWebView.getSettings().setLoadsImagesAutomatically(true);
//如果访问的页面中有 Javascript,则 WebView 必须设置支持 Javascript
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
// Configure the client to use when opening URLs
myWebView.setWebViewClient(new MyBrowser());
// Load the initial URL
myWebView.loadUrl("http://h5.m.taobao.com/awp/core/detail.htm?id=563621033487");
}
// 如果希望点击链接继续在当前browser中响应,而不是新开Android的系统browser中响应该链接,必须覆盖 WebView的WebViewClient对象.
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//view.loadUrl(url);
return false;
}
}
/*如果不做任何处理 ,浏览网页,点击系统“Back”键,整个 Browser 会调用 finish()而结束自身,
如果希望浏览的网页回退而不是推出浏览器,需要在当前Activity中处理并消费掉该 Back 事件.(代码有些精简)*/
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
URL 语法
URL由三部分组成:资源类型、存放资源的主机域名、资源文件名。
对淘宝的自定义协议进行处理
URL的一般语法格式为:
(带方括号[]的为可选项):
protocol :// hostname[:port] / path / [;parameters][?query]#fragment
public static class CommonWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("leo", "[webViewUrl]" + url);
if (url.startsWith("tel")) {// 机构电话
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
Uri uri = Uri.parse(url);
if ("baonajia".equals(uri.getScheme())) {
if ("organization".equals(uri.getHost())) {// 课程详情跳转到机构详情
Organization organization = new Organization();
organization.setId(uri.getQueryParameter("organization_id"));
organization.setName(uri.getQueryParameter("organization_name"));
Intent intent = new Intent(view.getContext(), OrganizationDetailActivity.class);
intent.putExtra(Const.EXTRA_ORG, organization);
view.getContext().startActivity(intent);
} else if ("course".equals(uri.getHost())) {// 机构详情跳转到课程详情
new CourseTask(view, uri.getQueryParameter("course_id")).execute();
} else if ("school".equals(uri.getHost())) {// 机构详情跳转到校区列表
String orgId = uri.getQueryParameter("organization_id");
Intent intent = new Intent(view.getContext(), SchoolListActivity.class);
intent.putExtra(Const.EXTRA_ORG_ID, orgId);
view.getContext().startActivity(intent);
} else if ("album".equals(uri.getHost())) {// 机构详情跳转到相册列表
String orgId = uri.getQueryParameter("organization_id");
Intent intent = new Intent(view.getContext(), PhotoAlbumListActivity.class);
intent.putExtra(Const.EXTRA_ORG_ID, orgId);
view.getContext().startActivity(intent);
}
return true;
} else {
view.loadUrl(url);
return true;
}
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG,
String.format("errorCode: %s, description: %s, when open: %s", errorCode, description, failingUrl));
view.loadUrl(URLCenter.getError());
}
}