我把闹钟定在午夜。但是设置警报会触发我的广播接收器?我在应用程序启动时设置了闹钟。编码:
//set the alarm
protected void onCreate(Bundle savedInstanceState) {
as=new AlarmSetter();
as.SetAlarma(this); .........
//the AlarmSetter
public class AlarmSetter {
public void SetAlarma(Context context){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
Intent intent = new Intent(context, AlarmKicked.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1333333, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,
pendingIntent);
}
}
//and my Broadcast reciever
public class AlarmKicked extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
NotificationsA.Notizer(context);
System.out.println("IT IS MIDNIGHT! SETTING TIMER..");
}
}
//Manifest
<receiver android:name=".AlarmKicked" android:process=":remote"/>
当我启动应用程序时,我设置了 Allarm.. 但同时我进入了 Logcat:它是午夜!设置计时器.. 是什么在开始时触发了我的广播接收器?
编辑:
作品!:
public void SetAlarma(Context context){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.AM_PM, Calendar.AM);