var AlarmManager;
var PendingIntent;
var Intent;
var IntentFilter;
var Context;
var Calendar;
var main;
var System;
var receiver;
var ALARM_EVENT = "com.kkaass.alarm";
mui.plusReady(function(){
AlarmManager = plus.android.importClass('android.app.AlarmManager');
PendingIntent = plus.android.importClass('android.app.PendingIntent');
Intent = plus.android.importClass('android.content.Intent');
IntentFilter = plus.android.importClass('android.content.IntentFilter');
Context = plus.android.importClass('android.content.Context');
receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver',{
onReceive:function(context,intent){
if(intent != null){
console.log('闹铃来了');
SetAlarm();
}
}
});
main = plus.android.runtimeMainActivity();
Calendar = plus.android.importClass('java.util.Calendar');
System = plus.android.importClass('java.lang.System');
var intentfilter = new IntentFilter();
intentfilter.addAction(ALARM_EVENT);
main.registerReceiver(receiver,intentfilter)
});
function SetAlarm(){
var intent = new Intent(ALARM_EVENT);
var pendingintent = PendingIntent.getBroadcast(main,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
var alarmManager =main.getSystemService(Context.ALARM_SERVICE);
var calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND,20);
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingintent);//setAndAllowWhileIdle方法API>=23才能使用
}