重启2016年后,android alarmmanager报警

fer*_*fer 2 android reboot android-alarms android-studio

我制作应用程序,您可以在其中设置闹钟.例如,我星期一早上7点钟上课,所以我需要每周一开始报警,但周二还有另一节课,必须这样做.

我已经可以做到这一点并且工作,每个curso发出警报,但是当我重新启动手机时,它们就无法工作.

这是我的代码,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 ++
Run Code Online (Sandbox Code Playgroud)

接收器

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);
Run Code Online (Sandbox Code Playgroud)

}

Manifets

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

  </receiver>
  <service android:name=".AlarmService" />
Run Code Online (Sandbox Code Playgroud)

因此,在我的应用程序的其他部分,我使用从json获取的数据设置警报.所有的工作,我想要唯一的问题是当我重新启动手机?

Mar*_*rat 10

根据developer.google.com

设备处于休眠状态时会保留已注册的警报(如果设备在此期间关闭,则可以选择将设备唤醒),但如果设备关闭并重新启动,则会清除设备.

因此,您需要创建另一个可重新创建所有警报的接收器.此接收器不是您的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>
Run Code Online (Sandbox Code Playgroud)

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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

    }
}
Run Code Online (Sandbox Code Playgroud)