1

I need to run WebView in a Service. The markup containing the WebView I take from browser.xml. Here is my code snippet:

public class OverlayWindow extends Service {
    private WindowManager windowManager;
    private View browserView;
    private WebView webView;
    private WindowManager.LayoutParams browserViewParams;

    @Override
    public void onCreate() {
        super.onCreate();

        browserView = layoutInflater.inflate(R.layout.browser, null);
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        webView = (WebView) browserView.findViewById(R.id.webView);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    webView.loadUrl(url);
                return true;
            }
        }

        browserViewParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                PixelFormat.TRANSLUCENT);

        windowManager.addView(browserView, browserViewParams);
}

Everything works well, but often the application crashes with the error:

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
        at android.app.Dialog.show(Dialog.java:281)
        at android.app.AlertDialog$Builder.show(AlertDialog.java:951)

The reason for this error is that the Context, but in my case I don't set the Context to WebView.

How this problem can be solved?

Thanks.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
Mark Korzhov
  • 2,109
  • 11
  • 31
  • 63
  • possible duplicate of [Can WebView be used inside a service?](http://stackoverflow.com/questions/15455129/can-webview-be-used-inside-a-service) – An SO User Mar 15 '15 at 19:19
  • What version of android are you on ? Usually, you should avoid using a webview in a Service, as it does not work very well. – Jonas Czech Mar 15 '15 at 19:29
  • Try to put an empty `onRecievedError()` method in your webViewClient (override it), as the default implementation can result in crashes if running in a service. Also, **dont** unconditionally override shouldOverrideUrlLoading, as it [can result in errors](http://stackoverflow.com/a/28430997/4428462). – Jonas Czech Mar 15 '15 at 19:38
  • @LittleChild , No it is not a duplicate, since the OP is asking about a specific error message. – Jonas Czech Mar 15 '15 at 19:46
  • Thank you for your answers! @JonasCz I have two tablet on Android 4.4.4. At one tablet works fine, but error occurs on another. But `onRecievedError()` doesn't catch the error when the page loads. – Mark Korzhov Mar 16 '15 at 07:25
  • have you seen my answer ? where have you reached with this ? would appreciate if you reply ... thanks :) – Yash Sampat May 07 '16 at 11:41

1 Answers1

4

I can offer another suggestion. Instead of opening the WebView directly, how about creating an Activity that has a WebView in its XML layout, and starting this Activity from the Service using

Intent intent = new Intent(getBaseContext(), WebViewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(intent);

This would eliminate the possibility of a BadTokenException permanently. Also, the URL for the WebView can be passed as an extra in the Intent from the Service to the Activity.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120