Skip to content

Instantly share code, notes, and snippets.

@arjuntraj
Created February 16, 2012 04:59
Show Gist options
  • Select an option

  • Save arjuntraj/1842171 to your computer and use it in GitHub Desktop.

Select an option

Save arjuntraj/1842171 to your computer and use it in GitHub Desktop.
Normal javascript ALERT to Native alert- converter Plugin for Android (phonegap application)

PhoneGap automatic Native Alert Plugin for android

by Arjun T Raj DESCRIPTION

This plugin provides a simple way to use the normal javascript alert it automaticaly convert to Native android alert, It does comply with the latest phonegap standards.

Compared to the phonegaps navigator.notification.alert if anyone (new to phonegap ) forget to change normal alert('') it will give a big problem. so here You can fell free to use normal alert. or MY native alert.

SETUP

Using this plugin requires android PhoneGap.

<plugin name="msgbox" value="com.company.msgbox.msgbox"/>

add this to your plugin.xml Add the .js files to your www folder on disk, and add reference(s) to the .js files as tags in your html file(s) Add

<script>(function(window){window.alert=function(text,opo){ navigator.nativeAlert.show(text);};}(window));</script>

this to the head of your html pages

JAVASCRIPT INTERFACE

// Basic with title navigator.nativeAlert.show('Your message');

// Complex feel free to """""""use Normal alert('hello world');"""""""""""""" this script automayicaly convert it to native alert Check source for additional configuration.

BUGS AND CONTRIBUTIONS

phpmyweb.

The latest code (my fork) will always be here LICENSE

Copyright 2012 Arjun T Raj. All rights reserved.

The MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. CREDITS

package com.company.msgbox;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.DialogInterface;
import android.app.AlertDialog;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;
public class msgbox extends Plugin {
private static final String SHOW = "show";
private static final int MSG_INDEX = 0;
private String msg;
@Override
public PluginResult execute(String arg0, final JSONArray arg1, String arg2) {
if ( arg0.equals(SHOW) )
{
this.ctx.runOnUiThread(new Runnable()
{
public void run()
{
// try/catch generated by editor
try {
msg = arg1.getString(MSG_INDEX);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
AlertDialog alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage(msg);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
});
}
return new PluginResult(Status.OK);
}
}
var msgbox = function() {
}
msgbox.prototype.show = function(str, success, fail) {
PhoneGap.exec(success, fail, "msgbox", "show", [str]);
};
navigator.nativeAlert = new msgbox();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment