安卓AlarmManager在重启后的闹钟设置问题(2016)

3

我正在制作一个应用程序,可以设置闹钟。

例如,我星期一有7点钟的课程,所以我需要每个星期一启动闹钟,但是我星期二也有另一门课程,也需要这样做。

我已经能够实现每个课程都有一个闹钟,并且可以正常工作,但是当我重新启动手机时,它们就无法工作了。

这是我的代码,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获取的数据设置闹铃。一切都像我想要的那样工作,唯一的问题是当我重新启动手机后会怎么样?

3个回答

12
根据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>

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

    }
}

好的,谢谢。但是在每个闹钟意图中,我设置了一个额外的参数 "intent.putExtra("name", curso);" 它只是一个字符串。但是它是每门课程的名称,我需要它。 - fer
1
没问题。您可以在意图中添加任意数量的额外信息。如果您觉得这个答案有帮助,请接受并点赞。我会非常感激 :) - Marat
非常棒的回答。感谢您抽出时间写下它。 - most venerable sir

0

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

Android开发者在这里给出了答案。


0

你可以监听重启事件,然后在手机重启时执行想要的操作

在你的清单文件中

<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
    }
}

1
我需要创建一个新的接收器吗?还是应该修改我的<receiver android:name=".AlarmReceiver" android:enabled="true" >? - fer
public void onReceive(Context context, Intent intent) { //在这部分中,我需要从put extra中获取数据,该如何获取????? } - fer
YourBootReceiver是一个新的接收器,用于接收手机重启事件。当手机重启完成时,会触发onReceive函数,在该函数中,您可以再次设置闹钟。我不知道您需要获取哪些数据? - Fuyuba

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接