2011-04-11 58 views
4

我已设置闹钟,以在Android设备处于开机状态时提醒我。但是,当我关闭设备,并再次提醒警报不工作。你们可以请我建议我怎么能解决这个问题?当设备关闭并再次打开时,闹钟在android中不起作用

我的代码看起来像这样,

Intent myIntent = new Intent(getApplicationContext(), serviceclass.class); 
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 
    CONST+id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

Calendar calender = Calendar.getInstance(); 
calender.setTimeInMillis(System.currentTimeMillis()); 
calender.set(Calendar.HOUR_OF_DAY, hours); 
calender.set(Calendar.MINUTE, ireminder.getMin()); 
calender.set(Calendar.SECOND, 0); 
calender.set(Calendar.MILLISECOND, 0); 
calender.set(Calendar.DAY_OF_WEEK, day); 

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 

calender.getTimeInMillis(), 7 * AlarmManager.INTERVAL_DAY, pendingIntent);     

回答

4

警报将在重新启动被清除。

你可以做的是

  1. 坚持一个数据库表报警信息的REBOOT_COMPLETED - 活动
  2. 在重新启动
  3. 寄存器,启动一个后台线程重新注册报警。确保您正确计算闹钟时间。

请参阅API:“注册的警报在设备睡着时保留(如果在此期间关闭设备,可以选择唤醒设备),但是如果设备关闭并重新启动,则会被清除。 - http://developer.android.com/reference/android/app/AlarmManager.html

1

创建内部清单

<receiver android:name=".OnBootReceiver" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 

类OnBootReceiver

public class OnBootReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     mgr.setRepeating(AlarmManager.RTC_WAKEUP, 
     SystemClock.currentThreadTimeMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, 
        pi); 

    } 

} 

您的活动中

sendBroadcast(new Intent(this, OnBootReceiver.class));