I had a similar problem: From within a web app, I'd like to use a native Android confirmation dialog. That implies that I have to call back from Android into the Javascript part with the result of the confirmation dialog.
I solved this as follows:
function foo() {
// user confirmation needed
var dataString = <encode data into string>;
MyClient.showConfirmationDialog('myCallBackFunction', dataString, 'A title', 'A message');
}
The code above calls the Android javascript interface (see below). The javascript provides the callback method myCallbackFunction()
, the name of which is passed to Android as parameter (along with a data string, a title and a message). The callback function looks as follows:
function myCallbackFunction(dataString, result) {
var data = <decode data from dataString>;
if (result) {
// user has confirmed
} else {
// user has denied
}
}
On the Android side, I first activate the Javascript interface in the Activity's onCreate()
method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
setContentView(webView);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJavascriptInterface(webView), "MyClient");
}
The implementation of MyJavascriptInterface
then creates the according Android dialog and passes the result back to javascript:
WebView webView;
public MyJavascriptInterface(WebView w) {
this.webView = w;
}
@JavascriptInterface
public void showConfirmationDialog(final String callbackFunction, final String data, String title,
String message) {
Dialog.OnClickListener positiveListener = new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
webView.loadUrl("javascript:" + callbackFunction + "('" + data + "', true)");
}
};
Dialog.OnClickListener negativeListener = new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
webView.loadUrl("javascript:" + callbackFunction + "('" + data + "', false)");
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(title).setMessage(message).setPositiveButton("Ok", positiveListener)
.setNegativeButton("Cancel", negativeListener).setCancelable(false);
builder.create().show();
}
Passing the callback function's name to Android allows to use several calls to confirmation dialogs, each of which is equipped with an own function do the actual action. The data string will carry all data needed to perform the action (and can even contain Json-encoded objects).