Kumarjit Chakraborty
Kumarjit Chakraborty

Reputation: 11

JavaScript Alert in Android WebView working only at first time

I am new to Stack Exchange and Andorid development.

I am working on Android webview. I have the following code in my activity class.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    WebView wv;
    WebSettings ws;

    try {
      wv = (WebView) findViewById(R.id.webview);
      ws = wv.getSettings();

      ws.setJavaScriptCanOpenWindowsAutomatically(true);
      ws.setJavaScriptEnabled(true);

      wv.clearCache(true);
      wv.loadUrl("http://<ip address>:<port>/<context>");
    } catch (Exception e) {
      e.printStackTrace();
    }
}

in layout-main.xml:

<WebView 
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />

in AndroidManifest.xml:

<uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".POCActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

In the url I have index.html with the following code:

<!DOCTYPE html>
<html>
  <head>
    <title>Contact Example</title>
    <script type="text/javascript">
        alert("Start");
    </script>
  </head>
  <body>
    <p>Database 1</p>
    <div id="test"></div>
  </body>
</html>

Working Environment: - Eclipse indigo - Android SDK min version 10 - Build Target 2.3.3

But the android code is working only once i.e. when I create a new android project and run the same, I can see the javascript alert appearing. From next time the javascript alert is not displayed. Even is any text changes(Say I modified "Database 1" to "Database 2") in the html page is also not displayed.

I tried the following: - Cleared appcache - Uninstalled the application and then ran the project again - Cleared

Please let me know what I am doing wrong here. Any help will be much appreciated.

Upvotes: 1

Views: 4867

Answers (2)

Siu
Siu

Reputation: 1432

This is a very old question, but I recently encountered the same problem and would like to share the solution to anyone who come across this later.

You will need to assign a custom WebChromeClient to your WebView to handle the alert.

mWebView.webChromeClient = object : WebChromeClient() {
    override fun onJsAlert(view: WebView?, url: String?, message: String?, result: JsResult?): Boolean {
        val alertDialog = AlertDialog.Builder(context)
                    .setMessage(message)
                    .setPositiveButton("OK") { dialogInterface, _ ->
                        dialogInterface.dismiss()
                        result?.confirm()
                    }
                    .setOnCancelListener { result?.cancel() }
                    .setCancelable(true)
                    .create()
        alertDialog.setCanceledOnTouchOutside(true)
        alertDialog.show()

        //Here AlertDialog is just an example. You can also simply show a Toast.
        //But remember to call JsResult.confirm() or JsResult.cancel()

        return true
    }
}

You need to call either JsResult.confirm() or JsResult.cancel() to notify your WebView whether the user confirmed or canceled the alert dialog. Otherwise, the WebView would assume an alert dialog is still showing(blocking the UI) and won't allow a new one to be shown.

Upvotes: 0

Masochist
Masochist

Reputation: 399

webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());   

Used these on my code and then my alert() worked pefectly

Upvotes: 2

Related Questions