From the PhoneGap download earlier, we need the following two files:
- Android/phonegap-1.0.0.jar
- Android/phonegap-1.0.0.js
In the root directory of the project you created in Eclipse, create two new directories:
- /libs
- /assets /www
Now copy
- Android/phonegap-1.0.0.jar to /libs
- Android/phonegap-1.0.0.js to /assets/www
In Eclipse, select the project in the Package Explorer and refresh (F5) the project. The copied file will appear in the project.
Now, create index.html in your www folder and add some code like so:
<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=320; user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>PhoneGap</title>
<link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title" charset="utf-8">
<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0rc2.js"></script>
<script type="text/javascript" charset="utf-8" src="phonegap-toast.js"></script>
<script type="text/javascript">
var ready = function() {
var win = function () {
//console.log("registered ");
},
fail = function() {
alert('Fail');
};
navigator.toast.showLong('bar');
};
document.addEventListener('deviceready', ready, false);
</script>
</head>
<body>
<button onclick="navigator.toast.cancel();">cancel</button>
</body>
</html>
(i)add master.css in the same folder asset/www/
(ii)add two more file in the same directory
*phonegap-1.0.0rc2.js
*phonegap-toast.js
Create two java class in the src/
1.> com.chariotsolutions/Toastify
package com.chariotsolutions;
import com.phonegap.*;
import android.os.Bundle;
public class Toastify extends DroidGap {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
}
2.> com.chariotsolutions.toast.plugin/ToastPlugin
package com.chariotsolutions.toast.plugin;
import android.util.Log;
import android.widget.Toast;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;
public class ToastPlugin extends Plugin {
private static final String TAG = "ToastPlugin";
private static final String LONG_TOAST_ACTION = "show_long";
private static final String CANCEL_ACTION = "cancel";
private static final int TOAST_MESSAGE_INDEX = 0;
private Toast toast = null;
private String toastMessage;
@Override
public PluginResult execute(String pluginAction, JSONArray data, String callbackId) {
Log.d(TAG, pluginAction);
if (pluginAction.equals(CANCEL_ACTION)) {
this.ctx.runOnUiThread(new Runnable() {
@Override
public void run() {
if (toast != null) toast.cancel();
}
});
} else {
try {
toastMessage = data.getString(TOAST_MESSAGE_INDEX);
} catch (JSONException e) {
Log.e(TAG, "Required parameter 'Toast Message' missing");
return new PluginResult(Status.ERROR);
}
if (pluginAction.equals(LONG_TOAST_ACTION)) {
Log.d(TAG, "long?");
this.ctx.runOnUiThread(new Runnable() {
@Override
public void run() {
toast = Toast.makeText(ctx, toastMessage, Toast.LENGTH_LONG);
toast.show();
}
});
} else {
this.ctx.runOnUiThread(new Runnable() {
@Override
public void run() {
toast = Toast.makeText(ctx, toastMessage, Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
return new PluginResult(Status.OK);
}
}
Finaly add the some code in Android Manifest file like
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chariotsolutions"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"
android:anyDensity="true"
/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:debuggable="true" android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:configChanges="orientation|keyboardHidden" android:name=".Toastify"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="plugin.ToastPlugin" android:value="com.chariotsolutions.toast.plugin.ToastPlugin"/>
</application>
</manifest>
This code definetly run
No comments:
Post a Comment