android开发package组件,android studio 开发 cordova plugin(组件)的 helloWorld

第一步:

按照 http://blog.csdn.net/u010919133/article/details/51507343 上创建一个android project

第二步:

已完成的project结构预览

0818b9ca8b590ca3270a3433284dd417.png

第三步:create MyPlugin.java文件

packageoo.mobile;

importandroid.content.Intent;

importorg.apache.cordova.CallbackContext;

importorg.apache.cordova.CordovaPlugin;

importorg.json.JSONArray;

importorg.json.JSONException;

importorg.json.JSONObject;

/*** Created by admin on 2016/5/27.*/public classMyPlugin extendsCordovaPlugin {

public static finalString ACTION_ADD_CALENDAR_ENTRY= "addCalendarEntry";

@Overridepublic booleanexecute(String action, JSONArray args, CallbackContext callbackContext) throwsJSONException {

try{

if(ACTION_ADD_CALENDAR_ENTRY.equals(action)) {

JSONObject arg_object = args.getJSONObject(0);

Intent calIntent = newIntent(Intent.ACTION_EDIT)

.setType("vnd.android.cursor.item/event")

.putExtra("beginTime", arg_object.getLong("startTimeMillis"))

.putExtra("endTime", arg_object.getLong("endTimeMillis"))

.putExtra("title", arg_object.getString("title"))

.putExtra("description", arg_object.getString("description"))

.putExtra("eventLocation", arg_object.getString("eventLocation"));

this.cordova.getActivity().startActivity(calIntent);

callbackContext.success();

return true;

}

callbackContext.error("Invalid action");

return false;

} catch(Exception e) {

System.err.println("Exception: "+ e.getMessage());

callbackContext.error(e.getMessage());

return false;

}

}

}

第四步:在config.xml注册MyPlugin

在里面添加如下代码:

添加的代码(需要注意的是你的packge name!!!!!):

添加后如图所示

0818b9ca8b590ca3270a3433284dd417.png

第五步:在assets/www/js文件夹下创建carrier.js(名字你喜欢就行)

varcalendarPlugin = {

createEvent: function(title, location, notes, startDate, endDate, successCallback, errorCallback) {

cordova.exec(

successCallback, // success callback functionerrorCallback, // error callback function'MyPlugin', // mapped to our native Java class called "CalendarPlugin"'addCalendarEntry', // with this action name[{ // and this array of custom arguments to create our entry"title": title,

"description": notes,

"eventLocation": location,

"startTimeMillis": startDate.getTime(),

"endTimeMillis": endDate.getTime()

}]

);

}

}

第六步:下面是index.html的实现(红色部分是添加的代码)

Hello World

Carrier Code!

function addToCal() {

var startDate = new Date("July 19, 2013 8:00:00");

var endDate = new Date("July 19, 2013 18:00:00");

var notes = "Arrive on time, don't want to miss out (from Android)";

var title = "PhoneGap Day";

var location = "Portland, OR";

var notes = "Arrive on time, don't want to miss out!";

var success = function() { alert("Success"); };

var error = function(message) { alert("Oopsie! " + message); };

calendarPlugin.createEvent(title, location, notes, startDate, endDate, success, error);

}

效果图

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值