Java tutorial
/* * * Copyright (C) 2012 Jonathan Parker <jonathanparkeremail@gmail.com> * * 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. * */ package com.phonegap.plugins; import org.apache.cordova.api.Plugin; import org.apache.cordova.api.PluginResult; import org.apache.cordova.api.PluginResult.Status; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.util.Log; import android.widget.Toast; public class Toaster extends Plugin { public static final String TOASTER = "show"; @Override public PluginResult execute(String action, JSONArray data, String callbackId) { PluginResult result = null; if (TOASTER.equals(action)) { try { JSONObject jo = data.getJSONObject(0); final String message = jo.getString("message"); this.cordova.getActivity().runOnUiThread(new Runnable() { @Override public void run() { ShowToast(message); } }); Log.d("Toaster", "Toast: " + message); result = new PluginResult(Status.OK); } catch (JSONException jsonEx) { Log.d("Toaster", "Got JSON Exception " + jsonEx.getMessage()); result = new PluginResult(Status.JSON_EXCEPTION); } } else { result = new PluginResult(Status.INVALID_ACTION); Log.d("Toaster", "Invalid action : " + action + " passed"); } return result; } private void ShowToast(String text) { Toast.makeText(this.cordova.getActivity(), text, Toast.LENGTH_LONG).show(); } }