源码:
var sender;
var alarm;
var main, receiver;
var ALARM_RECEIVER = "alarm_receiver";
mui.plusReady(function() {
if (plus.os.name.toUpperCase() == "ANDROID") {
main = plus.android.runtimeMainActivity();
// 广播接收
receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
onReceive: function(context, intent) { //实现onReceiver回调函数
console.log('闹钟响了');
main.unregisterReceiver(receiver); //取消监听
addAlarm();
}
});
addAlarm(); //启动闹钟
}
});
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');
if (alarm != null)
alarm.cancel(sender);
sender = PendingIntent.getBroadcast(main, 0, intent, 0);
//
var AlarmManager = plus.android.importClass("android.app.AlarmManager");
var Context = plus.android.importClass("android.content.Context");
alarm = main.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
}
4 个回复
6***@qq.com (作者) - 90后
源码:
var sender;
var alarm;
var main, receiver;
var ALARM_RECEIVER = "alarm_receiver";
mui.plusReady(function() {
});
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');
if (alarm != null)
alarm.cancel(sender);
sender = PendingIntent.getBroadcast(main, 0, intent, 0);
//
var AlarmManager = plus.android.importClass("android.app.AlarmManager");
var Context = plus.android.importClass("android.content.Context");
alarm = main.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
}
1***@qq.com - jackwei86
我也遇到了,真是不能随便更新版本,之前以切正常,更新下HBuilderX到最新,然后就
var Context = plus.android.importClass("android.content.Context");
var NetworkInfo = plus.android.importClass("android.net.NetworkInfo");
var ConnectivityManager = plus.android.importClass("android.net.ConnectivityManager");
var main = plus.android.runtimeMainActivity();
卡住了
2***@qq.com
我也遇到了,请问解决了吗
1***@qq.com
hbx3.6.4 又出现这个问题了.....
胖虎777
是啊,可恨的是sdk都到3.6.4了,不更新都不行
2022-11-01 13:30