报警器不工作在Android的?

问题描述:

我无法摆脱的东西,我想,是非常微不足道的。 基本上我在未来的调度报警了特定的时刻:

I get stuck with something that , I guess , is very trivial. Basically I am scheduling alarm for a given moment in the future :

Intent contentIntent = new Intent(this, AlarmReceiver.class); 
PendingIntent theappIntent = PendingIntent.getService(Main.this, 0,contentIntent, 0); 
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day, hour,minute); 
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), theappIntent); 

工作正常,而手机是不断地对。 我的问题是不会触发报警手机重新启动后, 并且在期望的时间导通。 有什么我错过了什么?我应该做一些额外的工作,以保持报警 计划启动后?

works fine while mobile is constantly on. My problem is that alarm is not triggered after mobile is restarted and is TURNED ON on the expected time. Is there something I missed? Should I do some extra work to keep alarm scheduled after restart?

您必须重置报警时,设备获取引导。 你缺少检测应用程序时,设备启动和复位低于alarm..so是code写在清单

You have to reset the alarm when device get boot. You are missing to detect the app when device boot and reset the alarm..so below is code written in manifest

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

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

此外,创建一个BroadcastReceiver收取开机完成意向

Also create a BroadcastReceiver to receive the boot completed intent

 public class YourReceiver extends BroadcastReceiver {


 Context ct;

 @Override
 public void onReceive(Context context, Intent intent) 
 {
    ct=context;
    if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
    {
      // Reset your alarm here
      Intent contentIntent = new Intent(ct, AlarmReceiver.class); 
      PendingIntent theappIntent = PendingIntent.getService(ct, 0,contentIntent, 0); 
      Calendar calendar = Calendar.getInstance();
      calendar.set(year, month, day, hour,minute); 
      AlarmManager am = (AlarmManager) ct.getSystemService(ALARM_SERVICE);
      am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), theappIntent); 
    }
 }