您的位置:首页 > 移动开发 > Android开发

phonegap-2.0+android教程

2014-05-04 20:59 330 查看
phonegap-2.0的安卓目录结构



1.创建新安卓项目

2.创建目录

/libs
assets/www/js

3.分别拷贝cordova-2.0.0.jar和cordova-2.0.0.js到libs(build-path) 和 www/js下

4.将xml整个文件夹拷打res目录下

5.AndroidManifest.xml 完全参考example里面的写法 

6. 创建Activity和plugins类

package com.lilin.myphonegap;

import org.apache.cordova.DroidGap;

import android.app.Activity;

import android.os.Bundle;

public class MyphonegapActivity 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");

        

        

    }

}

package com.lilin.myphonegap.plugins;

import org.apache.cordova.api.Plugin;

import org.apache.cordova.api.PluginResult;

import org.json.JSONArray;

import org.json.JSONObject;

public class MyTestPlugins extends Plugin{

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
//JSONObject arg = args.optJSONObject(0)
System.out.println("OK>>>>>>>>>>>>>>");;
System.out.println( action+">>" );
if("lilin_test".equalsIgnoreCase(action)){
return doTest();
}
return null;
}

private PluginResult doTest() {
PluginResult resutl = new PluginResult(PluginResult.Status.OK);
System.out.println( "OK222");
return resutl;
}

}

7.在xml的config.xml添加

<plugin name="lilin_test_service" value="com.lilin.myphonegap.plugins.MyTestPlugins"/>

8.在www的文件夹下创建index.html

<!DOCTYPE HTML>

<html>

<head>

<title>Cordova</title>

<script type="text/javascript" charset="utf-8" src="js/cordova-2.0.0.js"></script> 

<script type="text/javascript">

function testbtn(){
 
cordova.exec(successCallback,errorCallback,"lilin_test_service","lilin_test",[]);

};

function alertDismissed() {

    // do something

}

function successCallback(arg){
//alert("successCallback ok");
//alert(arg.recode+" : "+arg.msg);
navigator.notification.alert(
   'successCallback ok!',  // message
   alertDismissed,         // callback
   'Game Over',            // title
   'Done'                  // buttonName
);

};

function errorCallback(msg){
alert(msg);

};

</script>

</head>

<body>

<input type="button" onclick="testbtn()" value="testbtn"></input>

</body>

</html>

9.项目预览



参考文章  http://docs.phonegap.com/en/2.0.0rc1/guide_getting-started_android_index.md.html#Getting%20Started%20with%20Android

jar下载地址 https://github.com/phonegap/phonegap/zipball/2.0.0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息