【发布时间】:2017-12-28 20:13:19
【问题描述】:
我想使用 Chrome 自定义标签来正确处理域外的 URL。 这是代码
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
String url = request.getUrl().toString();
if(url.startWith("http://my.domain.name"))
return false;
else{
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.setToolbarColor(getResources().getColor(R.color.colorPrimary));
builder.setStartAnimations(getActivity(), R.anim.slide_in_right, R.anim.slide_out_left);
builder.setExitAnimations(getActivity(), R.anim.slide_in_left, R.anim.slide_out_right);
Intent actionIntent = new Intent(
getApplicationContext(), ActionBroadcastReceiver.class);
actionIntent.setData(Uri.parse(url));
PendingIntent menuItemPendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 0, actionIntent, 0);
builder.addMenuItem(getString(R.string.action_share), menuItemPendingIntent);
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(getActivity(), Uri.parse(url));
return true;
}
}
});
但是,当我单击域外 URL 时,偶尔会出现 ANR 对话框并且应用程序的 UI 冻结。
我尝试调试 anr 跟踪,但没有发现可疑线程。 ANR 跟踪文件很长,所以我把它贴在这里: https://gist.github.com/hoavt-54/42f1109c0619eed81e82a9a8d1128a6d
如果您对如何调试应用程序或如何理解跟踪文件有任何建议,我将不胜感激。
【问题讨论】:
-
在 ui 线程上运行?
-
@TWL:我很确定不是,但可能是
-
即使 shouldOverrideUrlLoading 在 UI 线程上运行,触发 ANR 对话框的时间也不应超过 5 秒。
-
Hoa:ANR 仅发生在主 (ui) 线程上。永远不要在 UI 线程上进行任何网络调用,绝对没有。
标签: android android-anr-dialog chrome-custom-tabs