AlarmManager在重启时重置

时间:2016-09-05 06:57:33

标签: java android android-service alarmmanager

我发出警报,在用户选择的时间打开活动。如果用户点击开始按钮,则警报会正常,但重启后会被取消。我到处都看,它说我应该使用服务。是否可以在重启后不使用服务来保持警报?我是编码的新手,所以请你详细说明我需要做什么。谢谢

public class MainActivity extends AppCompatActivity {

Button disable;
Button start;
TimePicker timePicker;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    timePicker = (TimePicker) findViewById(R.id.timePicker);
    disable = (Button) findViewById(R.id.disable_alarm);
    start = (Button) findViewById(R.id.button);


    start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
             Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, timePicker.getHour());
                calendar.set(Calendar.MINUTE, timePicker.getMinute());

                Intent intent = new Intent(getApplicationContext(),notification.class);

                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 120*1000, pendingIntent);

        }
    });

    disable.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getApplicationContext(),notification.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.cancel(pendingIntent);
        }
    });

和广播接收器

public class notification extends BroadcastReceiver {



@Override
public void onReceive(Context context, Intent intent) {

    Intent scheduledIntent = new Intent(context, pop_up2.class);
    scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(scheduledIntent);}}

由于

1 个答案:

答案 0 :(得分:1)

手机关机后,所有活动闹钟都会被取消。要重置警报,您需要使用单独的广播接收器,它将仅接收启动完成的操作。接收器然后启动服务,它将在后台重置所有警报。最适合你的是IntentService,因为它在工作完成后就会结束。当然,您需要在某处存储有关警报的信息,以便记住要重置的警报。例如,您可以使用SQLite来存储它们。

在你的清单中:

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

<receiver android:name="developer.marat.apps.days.Alarms.BootCompletedReceiver"
    android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<service android:name="developer.marat.apps.days.Alarms.RestartAlarmsService"/>

特殊接收者:

public class BootCompletedReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {

        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            Intent i = new Intent(context, RestartAlarmsService.class);
            ComponentName service = context.startService(i);
        }
    }
}

RestartAlarms:

public class RestartAlarmsService extends IntentService{

    public RestartAlarmsService() {
        super("RestartAlarmsService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        // Restart your alarms here.
        // open database, iterate through every alarm and set them again

    }
}