3

Here is my code, that worked fine in PRE lollipop.

public static void alertWebView(String url, Context c) {

     final WebView webView  = new WebView(c.getApplicationContext());
     webView.clearCache(true);
     webView.clearHistory();
     webView.getSettings().setJavaScriptEnabled(true);
     webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
     webView.getSettings().setDefaultTextEncodingName("utf-8");
     webView.getSettings().setPluginState(PluginState.ON);
     webView.setOnLongClickListener(new OnLongClickListener() {
         @Override
         public boolean onLongClick(View v) {
             return true;
         }
     });
     webView.setLongClickable(false);

     AlertDialog.Builder builder = new AlertDialog.Builder(c);
     builder.setInverseBackgroundForced(true);

     builder.setPositiveButton("Bezár", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0, int arg1) {
        }
     }).setView(webView);

     final AlertDialog dialog = builder.create();

     webView.setWebChromeClient(new WebChromeClient() {
           @Override
           public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
           }
     });

     webView.setWebViewClient(new WebViewClient() {
       public void onPageFinished(WebView view, String url) {
       }
     });

     webView.loadUrl(url);
     mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                dialog.show();                  
            }
    }, 1000);

    dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

}

04-21 11:18:48.425: W/JsDialogHelper(3997): Cannot create a dialog, the WebView context is not an Activity

the html is a button with an onclick and a confirm js dialog.

lacas
  • 13,928
  • 30
  • 109
  • 183
  • I recommend reading: [Showing an AlertDialog from a Webview outside of an Activity](http://stackoverflow.com/questions/26044179/showing-an-alertdialog-from-a-webview-outside-of-an-activity). – Ziem Apr 21 '15 at 09:22
  • I'm surprised it worked before L. AlertDialogs require an activity context for its window token. Unless you do what the above linked SO question did, anyways. But I think your problem stems from `new WebView(c.getApplicationContext());` not being an activity context. – EpicPandaForce Apr 21 '15 at 09:30

0 Answers0