1、新建一个java类,继承plugin类。重写它的execute方法:
publicclass pluginTest01 extends Plugin{
private Object synObj = new Object();
@Override
public PluginResult execute(String action, JSONArray data, String callbackID) {
// TODO Auto-generated method stub
PluginResult result = null;
try {
PluginResult.Status status = PluginResult.Status.OK;
if(action.equals("getsubject")){
Log.e("test", "test plugin js -> java~~~~"+data.getString(0));
Log.e("test", "test plugin js -> java~~~~");
//result = new PluginResult(status, data.getString(0));
handler.sendEmptyMessage(1);
sleep();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
接着,自定义一个handler处理新页面的跳转工作,对于为什么要用handler我的理解是主线程被占了~~~:
private Handler handler = new Handler() {
publicvoid handleMessage(Message msg) {
if (msg == null) {
return;
}
switch (msg.what) {
case 1:
Intent intent = new Intent(ctx.getBaseContext(), Temp.class);//你想去的activity(exp:Temp)
ctx.startActivityForResult(pluginTest01.this, intent, 1);
break;
}
};
};
然后,重写plugin类的onActivityResult方法,这个方法在返回webview时调用:
@Override
publicvoid onActivityResult(int requestCode, int resultCode, Intent intent) {
// TODO Auto-generated method stub
if(requestCode==1){
if(resultCode == Activity.RESULT_CANCELED){
//返回时调用
Log.e("test", "result plugin -> java~~~~");
Log.e("test", "result plugin -> java~~~~");
weakup();
}
}else{
super.onActivityResult(requestCode, resultCode, intent);
}
}
下面自己构造sleep()和weakup(),不多解释:
privatevoid sleep()
{
try
{
synchronized(synObj)
{
synObj.wait();
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
privatevoid weakup()
{
synchronized(synObj)
{
synObj.notify();
}
}
}
这样,一个phonegap插件就写好了。
2、在plugin.xml中加入以下代码,注册这个插件:
<plugin name="pluginTest01" value="wgj.plugin.pluginTest01"/>
解释:name-自己起的别名,value-真实的包名.类名
3、在phonegap提供的js文件(位置:assets/www/xxx.js)最后面加入一段代码:
var testAndroid01API=function(){};
testAndroid01API.prototype.getsubject = function(params, success, fail)
{
return PhoneGap.exec(
function(args){ success(args); },
function(args){ fail(args); },
'pluginTest01', //java类
'getsubject', //action
[params] //params
);
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin('testAndroid01API', new testAndroid01API());
});
4、自己写个html页面,放在assets/www/文件夹下,贴个我的测试页面代码:
<!DOCTYPE HTML>
<html>
<head>
<title>PhoneGap</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript">
function test01(){
var subject = document.getElementById("text1").value
window.plugins.testAndroid01API.getsubject(subject,
function(r){//alert(r);
},
function(e){//alert(e);
}
);
}
</script>
</head>
<body>
<h1>Hello World</h1>
<input type="hidden" name="text1" id="text1" value="110"/>
<button type="button">Click Me!</button>
</body>
</html>
5、新建一个android起始activity,继承DroidGap:
import android.os.Bundle;
import com.phonegap.*;
publicclass PhonegapDemo extends DroidGap {
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("[url=]file:///android_asset/www/index.html[/url]");
}
}
大功告成啦,点击html中的button就可以跳转到你想去的activity了。
publicclass pluginTest01 extends Plugin{
private Object synObj = new Object();
@Override
public PluginResult execute(String action, JSONArray data, String callbackID) {
// TODO Auto-generated method stub
PluginResult result = null;
try {
PluginResult.Status status = PluginResult.Status.OK;
if(action.equals("getsubject")){
Log.e("test", "test plugin js -> java~~~~"+data.getString(0));
Log.e("test", "test plugin js -> java~~~~");
//result = new PluginResult(status, data.getString(0));
handler.sendEmptyMessage(1);
sleep();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
接着,自定义一个handler处理新页面的跳转工作,对于为什么要用handler我的理解是主线程被占了~~~:
private Handler handler = new Handler() {
publicvoid handleMessage(Message msg) {
if (msg == null) {
return;
}
switch (msg.what) {
case 1:
Intent intent = new Intent(ctx.getBaseContext(), Temp.class);//你想去的activity(exp:Temp)
ctx.startActivityForResult(pluginTest01.this, intent, 1);
break;
}
};
};
然后,重写plugin类的onActivityResult方法,这个方法在返回webview时调用:
@Override
publicvoid onActivityResult(int requestCode, int resultCode, Intent intent) {
// TODO Auto-generated method stub
if(requestCode==1){
if(resultCode == Activity.RESULT_CANCELED){
//返回时调用
Log.e("test", "result plugin -> java~~~~");
Log.e("test", "result plugin -> java~~~~");
weakup();
}
}else{
super.onActivityResult(requestCode, resultCode, intent);
}
}
下面自己构造sleep()和weakup(),不多解释:
privatevoid sleep()
{
try
{
synchronized(synObj)
{
synObj.wait();
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
privatevoid weakup()
{
synchronized(synObj)
{
synObj.notify();
}
}
}
这样,一个phonegap插件就写好了。
2、在plugin.xml中加入以下代码,注册这个插件:
<plugin name="pluginTest01" value="wgj.plugin.pluginTest01"/>
解释:name-自己起的别名,value-真实的包名.类名
3、在phonegap提供的js文件(位置:assets/www/xxx.js)最后面加入一段代码:
var testAndroid01API=function(){};
testAndroid01API.prototype.getsubject = function(params, success, fail)
{
return PhoneGap.exec(
function(args){ success(args); },
function(args){ fail(args); },
'pluginTest01', //java类
'getsubject', //action
[params] //params
);
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin('testAndroid01API', new testAndroid01API());
});
4、自己写个html页面,放在assets/www/文件夹下,贴个我的测试页面代码:
<!DOCTYPE HTML>
<html>
<head>
<title>PhoneGap</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript">
function test01(){
var subject = document.getElementById("text1").value
window.plugins.testAndroid01API.getsubject(subject,
function(r){//alert(r);
},
function(e){//alert(e);
}
);
}
</script>
</head>
<body>
<h1>Hello World</h1>
<input type="hidden" name="text1" id="text1" value="110"/>
<button type="button">Click Me!</button>
</body>
</html>
5、新建一个android起始activity,继承DroidGap:
import android.os.Bundle;
import com.phonegap.*;
publicclass PhonegapDemo extends DroidGap {
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("[url=]file:///android_asset/www/index.html[/url]");
}
}
大功告成啦,点击html中的button就可以跳转到你想去的activity了。