项目需要,做了一个闹铃提醒的例子。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title>TTimer</title>
<script src="js/mui.min.js"></script>
<link href="http://ask.dcloud.net.cn/css/mui.min.css" rel="stylesheet" />
</head>
<body style="padding: 40px;">
<button id="alarm"> 插入闹铃</button>
</body>
<script type="text/javascript" charset="utf-8">
mui.init();
mui.plusReady(function() {
//
mui("body").on('tap', 'button', function(event) {
var id = this.getAttribute('id');
if (id == "alarm") {
addAlarm();
}
});
var main = plus.android.runtimeMainActivity();
// 广播接收
var ALARM_RECEIVER = "alarm_receiver";
var receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
onReceive: function(context, intent) { //实现onReceiver回调函数
plus.android.importClass(intent); //通过intent实例引入intent类,方便以后的‘.’操作
//console.log(intent.getAction()); //获取action
var txt = "接收到消息" intent.getAction() ": " getCurTime();
mui.toast(txt);
pushMsg(txt);
main.unregisterReceiver(receiver); //取消监听
}
});
//
function addAlarm() {
// --- 注册监听 start ---
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
var filter = new IntentFilter(ALARM_RECEIVER);
main.registerReceiver(receiver, filter);
// --- 注册监听 end ---
// --- 设置闹铃时间 start ---
var Calendar = plus.android.importClass('java.util.Calendar');
var calendar = Calendar.getInstance();
// 11点4分执行
//calendar.set(Calendar.HOUR_OF_DAY, 11);
//calendar.set(Calendar.MINUTE, 4);
//calendar.set(Calendar.SECOND, 0);
// 过5s 执行
calendar.setTimeInMillis(Date.parse(new Date()));
calendar.add(Calendar.SECOND, 5);
// --- 设置闹铃时间 end ---
// --- 设置闹铃 ---
var Intent = plus.android.importClass('android.content.Intent');
var intent = new Intent(ALARM_RECEIVER);
//
var PendingIntent = plus.android.importClass('android.app.PendingIntent');
var sender = PendingIntent.getBroadcast(main, 0, intent, 0);
//
var AlarmManager = plus.android.importClass("android.app.AlarmManager");
var Context = plus.android.importClass("android.content.Context");
var alarm = main.getSystemService(Context.ALARM_SERVICE);
// 一次
alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
// 重复多次
//alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5 * 1000, sender);
mui.toast("设置闹铃5秒后提醒: " getCurTime());
};
//
function pushMsg(content) {
var options = {
"title": "闹铃提醒",
};
plus.push.createMessage(content, "alarm", options);
};
//
function getCurTime() {
var d = new Date();
return d.getFullYear() "-" (d.getMonth() 1) "-" d.getDate() " " d.getHours() ":" d.getMinutes() ":" d.getSeconds();
};
});
</script>
</html>
11 个评论
要回复文章请先登录或注册
redzippo