Where The Streets Have No Name

phonegap 1.0.0에서 plug-in개발 본문

Developement/Mobile

phonegap 1.0.0에서 plug-in개발

highheat 2011. 8. 30. 16:24

참고 : http://kgriff.posterous.com/phonegap-10-android-plugin-updates
http://kgriff.posterous.com/building-a-phonegap-plugin-for-android
/res/xml/plugins.xml

<?xml version="1.0" encoding="utf-8"?>
<plugins>
    <plugin name="App" value="com.phonegap.App"/>
    <plugin name="Geolocation" value="com.phonegap.GeoBroker"/>
    <plugin name="Device" value="com.phonegap.Device"/>
    <plugin name="Accelerometer" value="com.phonegap.AccelListener"/>
    <plugin name="Compass" value="com.phonegap.CompassListener"/>
    <plugin name="Media" value="com.phonegap.AudioHandler"/>
    <plugin name="Camera" value="com.phonegap.CameraLauncher"/>
    <plugin name="Contacts" value="com.phonegap.ContactManager"/>
    <plugin name="Crypto" value="com.phonegap.CryptoHandler"/>
    <plugin name="File" value="com.phonegap.FileUtils"/>
    <plugin name="Network Status" value="com.phonegap.NetworkManager"/>
    <plugin name="Notification" value="com.phonegap.Notification"/>
    <plugin name="Storage" value="com.phonegap.Storage"/>
    <plugin name="Temperature" value="com.phonegap.TempListener"/>
    <plugin name="FileTransfer" value="com.phonegap.FileTransfer"/>
    <plugin name="Capture" value="com.phonegap.Capture"/>
    <plugin name="HelloWorld" value="net.nnn.phonegapdemo.HelloWorld"/>
    <plugin name="Toasty" value="net.nnn.phonegapdemo.ToastPlugin"/>
</plugins>

 

package net.nnn.phonegapdemo;

import org.json.JSONArray;
import org.json.JSONException;

import android.util.Log;
import android.widget.Toast;

import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;

public class ToastPlugin extends Plugin {
	
	private static final String TAG = "ToastPlugin";
	private static final String LONG_TOAST_ACTION = "show_long";
	private static final int TOAST_MESSAGE_INDEX = 0;

	@Override
	public PluginResult execute(String action, JSONArray data, String callinglbackId) {
		
		String toastMessage;
		try {
			toastMessage = data.getString(TOAST_MESSAGE_INDEX);
		} catch (JSONException e) {
			Log.e(TAG, "Required parameter 'Toast Message' missing");
			return new PluginResult(Status.ERROR);
		}

		if (action.equals(LONG_TOAST_ACTION)) {
		  ctx.runOnUiThread(new RunnableToast(toastMessage, Toast.LENGTH_LONG));
		} else {
		  ctx.runOnUiThread(new RunnableToast(toastMessage, Toast.LENGTH_SHORT));
		}

		return new PluginResult(Status.OK);
	}
	
	class RunnableToast implements Runnable {
		private String message;
		private int length;

		public RunnableToast(String message, int length) {
			this.message = message;
			this.length = length;
		}

		@Override
		public void run() {
			Toast.makeText(ctx, message, length).show();
		}
	}
}

 

var Toasty = function() {
};

Toasty.prototype.showLong = function(message, win, fail) {
  PhoneGap.exec(win, fail, "Toasty", "show_long", [message]);
};

Toasty.prototype.showShort = function(message, win, fail) {
  PhoneGap.exec(win, fail, "Toasty", "show_short", [message]);
};
navigator.tp = new Toasty();

 

<!DOCTYPE HTML>
<html>
<head>
<title>PhoneGap</title>
<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script type="text/javascript" charset="utf-8" src="hello.js"></script>
<script type="text/javascript" charset="utf-8" src="phonegap-toast.js"></script>
<script type="text/javascript" charset="utf-8">
var ready = function() {
            
	var win = function(d) {
		console.log('win callback fired with: ' + d);            
	}
            
	var fail = function(d) {
		console.log('fail callback fired with: ' + d);
	}
            
	hello('brian', win);
	hello(null, win, fail);
	navigator.tp.showLong('bar');
}
    
document.addEventListener("deviceready", ready, false);
</script>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>