手机重启后,android报警管理器无效

时间:2015-05-24 13:58:25

标签: android notifications alarmmanager

我正在使用以下代码提醒用户在特定时间使用通知关于应用中的新更新,每件事情都正常,我们会在所需的时间收到提醒

这是 mainactivity.java

public void setAlarm(){
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(  MainActivity.this, 0, alarmIntent, 0);

    alarmStartTime.set(Calendar.HOUR_OF_DAY, 10);
    alarmStartTime.set(Calendar.MINUTE, 00);
    alarmStartTime.set(Calendar.SECOND, 0);
    alarmManager.setRepeating(AlarmManager.RTC, alarmStartTime.getTimeInMillis(), getInterval(), pendingIntent);
}
private int getInterval(){
     int days = 1;
     int hours = 24;
     int minutes = 60;
     int seconds = 60;
     int milliseconds = 1000;
     int repeatMS = days * hours * minutes * seconds * milliseconds;
     return repeatMS;
}

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

    NotificationManager notificationManager;

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service1 = new Intent(context, AlarmService.class);
            context.startService(service1);
    }
}

和AlarmService.java

public class AlarmService extends Service { 
   private static final int NOTIFICATION_ID = 1;
   private NotificationManager notificationManager;
   private PendingIntent pendingIntent;

    @Override
    public IBinder onBind(Intent arg0)
    {
        return null;
    }

   @SuppressWarnings("static-access")
   @Override
   public void onStart(Intent intent, int startId)
   {
       super.onStart(intent, startId);
       Context context = this.getApplicationContext();
       notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
       Intent mIntent = new Intent(this, MainActivity.class);
    pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);     
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle("Bananas");
    builder.setContentText("get your bananas");
    builder.setSmallIcon(R.drawable.ic_launcher);
    builder.setContentIntent(pendingIntent);

    notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, builder.build());
    }
}

我的问题是,如果我们重新启动手机,以前所有关于闹钟的信息都会丢失,我们下次重启后就不会收到提醒,可以做的是警报管理器或通知的问题

3 个答案:

答案 0 :(得分:7)

您需要将此添加到 AndroidManifest.xml

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

 <receiver
            android:name="Your package name.AlarmReboot"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>

并添加一个广播接收器,一旦手机重新启动,它将再次设置闹钟:

public class AlarmReboot extends BroadcastReceiver{

}

答案 1 :(得分:1)

这解释得更好

http://javatechig.com/android/repeat-alarm-example-in-android

如何使用设备重启设置闹钟

答案 2 :(得分:0)

您可以使用setRepeating API作为setExact() API延迟setRepeating()意图,而不是使用AlarmManager sleep mode或{{} 1}}。