android alarmmanager 2016 重启后报警

android alarmmanager alarms after reboot 2016

我正在制作一个应用程序,您可以在其中设置闹钟。 比如我周一7点有class,所以我需要每周一启动闹钟,但我周二还有另一个class,也必须这样做。

我已经可以做到并工作了,每个光标都有一个警报,但是当我重新启动单元时phone然后它们就不起作用了。

这是我的代码,put extra 在我的应用程序中非常重要:

Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(System.currentTimeMillis());
    cal.set(Calendar.HOUR_OF_DAY, horai);
    cal.set(Calendar.MINUTE, minutoi);
    cal.set(Calendar.DAY_OF_WEEK, dias.getSelectedItemPosition() + 1);
    cal.add(Calendar.SECOND, 2);



    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    intent.putExtra("name", curso);
    //intent.putExtra("curos bn",1);
    PendingIntent pendingIntent =
           PendingIntent.getBroadcast(getBaseContext(),
           cont, intent, PendingIntent.FLAG_UPDATE_CURRENT );//cont star with 1 a then ++

接收者

public class AlarmReceiver extends BroadcastReceiver {

private static final String TAG = "BANANEALARM";
Intent intent;
PendingIntent pendingIntent;
NotificationManager notificationManager;
private static final int PERIOD=5000;
@Override
public void onReceive(Context context, Intent intent) {



    Log.i(TAG, "BroadcastReceiver has received alarm intent.");
    Intent service1 = new Intent(context, AlarmService.class);
    String id = intent.getStringExtra("name"); // this is so important
    service1.putExtra("name",id);
    context.startService(service1);

}

清单

  <receiver android:name=".AlarmReceiver"  android:enabled="true" >

  </receiver>
  <service android:name=".AlarmService" />

因此,在我的应用程序的其他部分,我使用从 json 获得的数据设置警报。 一切如我所愿,唯一的问题是当我重新启动 phone?

你可以监听重启事件,然后在phone重启

时做你想做的事

在您的清单中

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

...

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

您的 java 代码

public class YourBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //do what you want
    }
}

内核会在您的闹钟超时时触发闹钟,但重启后无法保存您的闹钟状态。因此,如果用户重启设备,您应该重置闹钟。

Android开发者给出答案here

根据developer.google.com

Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

因此,您将需要创建另一个接收器来重新创建所有警报。此接收器不是您的 AlarmReceiver,它不处理激活的警报。它仅用于在重启后重置它们。

为此,您需要这些代码行:

AndroidManifest.xml

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

<application

    <!-- rest of the code -->

    <receiver android:name=".AlarmBroadcastReceiver"/>

    <service android:name=".BootService"/>

    <receiver android:name=".RestartAlarmsReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

RestartAlarmsReceiver.java

public class RestartAlarmsReceiver extends BroadcastReceiver {

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

        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {

            // It is better to reset alarms using Background IntentService
            Intent i = new Intent(context, BootService.class);
            ComponentName service = context.startService(i);

            if (null == service) {
                // something really wrong here
                Log.e(TAG, "Could not start service ");
            }
            else {
                Log.e(TAG, "Successfully started service ");
            }

        } else {
        Log.e(TAG, "Received unexpected intent " + intent.toString());
        }
    }
}

BootService.java

public class BootService extends IntentService {

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

    @Override
    protected void onHandleIntent(Intent intent) {

        // Your code to reset alarms.
        // All of these will be done in Background and will not affect
        // on phone's performance

    }
}