यदि आप जावास्क्रिप्ट alert
फ़ंक्शन से संदेश बॉक्स प्रदर्शित करना चाहते हैं तो WebChromeClient
का उपयोग करना अनिवार्य नहीं है। आप अपने जावास्क्रिप्ट कोड और क्लाइंट-साइड एंड्रॉइड कोड के बीच इंटरफेस बना सकते हैं।
नीचे दिए गए उदाहरण में अपना JavaScript कोड बजाय जावास्क्रिप्ट का alert()
समारोह का उपयोग करने का एक Dialog
प्रदर्शित करने के लिए, अपने Android कोड में एक विधि कॉल कर सकते हैं। मैंने पाया कि यह अलर्ट प्रदर्शित करने का सबसे सुविधाजनक और व्यापक रूप से समर्थित तरीका है।
फ़ाइल:: WebAppInterface.java
import android.content.Context;
import android.webkit.JavascriptInterface;
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a Message box from the web page */
@JavascriptInterface
public void androidAlert(String message) {
DialogBox dbx = new DialogBox();
dbx.dialogBox(message, "I get it", "",mContext);
}
}
फ़ाइल: DialogBox.java
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
public class DialogBox {
public boolean dialogBox(String msg, String okButton, String cancelButton, final Context activity) {
Dialog v = null;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
alertDialogBuilder.setMessage(msg);
if (okButton != "") {
alertDialogBuilder.setPositiveButton(okButton,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) { /**/ }
});
}
if (cancelButton != "") {
alertDialogBuilder.setNegativeButton(cancelButton,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) { /**/ }
});
}
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
return true;
}
}
इसके बाद, बाँध
अपने Android एप्लिकेशन में निम्नलिखित वर्गों को शामिल करेंजावास्क्रिप्ट के लिए 10 वर्ग कि addJavascriptInterface()
के साथ अपने WebView
में चलता है और इंटरफेस Android
नाम:
WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
यह WebView
में चल रहा जावास्क्रिप्ट के लिए Android
कहा जाता है एक अंतरफलक पैदा करता है।इस बिंदु पर, आपके वेब एप्लिकेशन परWebAppInterface
कक्षा तक पहुंच है। नीचे कुछ HTML और जावास्क्रिप्ट कि जब उपयोगकर्ता एक बटन क्लिक करता है नए इंटरफ़ेस का उपयोग संदेश बॉक्स बनाता है: बटन पर क्लिक करें पर
<input type="button" value="Alert!" onClick="javascript:Android.androidAlert('It works!');" />
, Android
इंटरफ़ेस कॉलWebAppInterface.androidAlert()
विधि करने के लिए प्रयोग किया जाता है।
चेतावनी का एक बिट:addJavascriptInterface()
का उपयोग कर जावास्क्रिप्ट को आपके एंड्रॉइड एप्लिकेशन को नियंत्रित करने की अनुमति देता है। यह बहुत उपयोगी सुविधा या खतरनाक सुरक्षा समस्या हो सकती है। इस प्रकार, आपको addJavascriptInterface()
का उपयोग नहीं करना चाहिए जब तक कि आपने अपने WebView
में दिखाई देने वाले सभी HTML और जावास्क्रिप्ट को लिखा न हो।
http://stackoverflow.com/questions/8634319/how-to-set-different -title-for-alert-dialog-when-webview-page-loaded – Raghunandan
@ गणेश: आप एंड्रॉइड मूल अलर्टडिअलॉग (http://developer.android.com/reference/android/app/AlertDialog.html) दिखाना चाहते हैं। ? –