Get the ANR dialog when opening links from WebView in custom Chrome tabs. How do I debug this?

I want to use custom Chrome tabs to properly handle URLs from my domain. Here is the code

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;
        }
    }
});

However, when I click URLs outside the domain, sometimes I get the ANR dialog and the user interface of the application freezes.

I am trying to debug anr traces but have not found a suspicious thread. The ANR trace file is quite long, so I posted it here: https://gist.github.com/hoavt-54/42f1109c0619eed81e82a9a8d1128a6d

If you have any suggestions on how to debug the application or how to understand the trace file, I would really appreciate it.

+4
3

5 - 1- .

, , , . , (onXyz()) - , , ..

. ANR, , , , .

:

  • / ,
  • /

ANR .

+3

StrictMode atleast . , .

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectAll()
            .penaltyLog()
            .build());

-

penaltyLog() // to print log
penaltyDeath() // This will crash you App(so costly penalty)
penaltyDialog() // Show alert when something went lazy on Main thread

https://developer.android.com/reference/android/os/StrictMode.html

+2

URL- Webview Chrome.

webview chrome thread, .

   @Override
        public void retainOldWebView() {
            if (mmtWebViewPop != null) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (mmtWebViewPop != null) {
                            webViewFrameLayout.removeView(mmtWebViewPop);
                            mmtWebViewPop = null;
                        }
  //TODO: write your own implementation here
                    }
                }, 100);
            }
        }

-,

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    WebView.setWebContentsDebuggingEnabled(true);
}

, , customChromeTabs WebView . .

- : -

  • WebViewActivity, url , ChromeTabActivity URL-, .
  • ChromeTabActivity, customChromeTab , - , .
  • ChromeTabActivity , , fb google , webview frameLayout, , pageload, .

            if (null != mmtWebView) {
                mmtWebView.setWebChromeClient(new WebChromeClientImpl(getApplicationContext()));
            }
    

WebChromeClientImpl WebChromeClient {

private final Context appContext;

public WebChromeClientImpl(Context appContext) {
    this.appContext = appContext;
}

@Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
    mmtWebViewPop = new WebView(view.getContext());
    mmtWebViewPop.setVerticalScrollBarEnabled(false);
    mmtWebViewPop.setHorizontalScrollBarEnabled(false);
    mmtWebViewPop.setWebViewClient(new WebViewClientImpl(appContext, null));
    mmtWebViewPop.getSettings().setJavaScriptEnabled(true);
    mmtWebViewPop.setLayoutParams(new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    webViewFrameLayout.addView(mmtWebViewPop);
    WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
    transport.setWebView(mmtWebViewPop);
    resultMsg.sendToTarget();
    return true;
}

@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
    return super.onConsoleMessage(consoleMessage);
}

@Override
public void onCloseWindow(WebView window) {
    super.onCloseWindow(window);
    retainOldWebView();
}

}

0

Source: https://habr.com/ru/post/1682052/


All Articles