如何保证AlarmManager生存手机重启和应用杀重启、手机、AlarmManager

2023-09-04 05:03:48 作者:凉了时光病了心脏

我有一个Android应用程序,我想在白天发送通知给用户在固定时间

I have an Android application where I want to send a notification to the user at a fixed time during the day

对于这一点,我使用AlarmManager安排报警,然后用它来生成通知

For this, I am using AlarmManager to schedule an alarm and then use that to generate notifications

不过,我现在面临的问题是,如果用户强制关闭的应用程序,该警报被重置,所以我没有收到任何通知。这是一个正常的行为?有没有什么办法出去吗?

But I am facing issue that if the user "Force Closed" the app, the Alarms is reset and hence I don't receive any notifications. Is this is a normal behaviour? Is there is any way out for this?

此外,报警也被当手机重新启动取消。如何控制呢?我需要听手机重启事件,并重新安排这些报警呢?那是这里唯一的选择?

Also, the alarms also gets cancelled if the phone restarts. How do I control that? Do I need to listen to the Phone Reboot event and again schedule the alarm there? Is that the only option here?

推荐答案

我在我的数据库中的表来存储我的所有未决报警的情况下重新启动了。如果手机重新启动我有一个服务类重置报警。

I have a table in my database to store all my pending alarms in case of a reboot. If the phone is rebooted I have a Service Class that resets the alarms.

private AppDatabase mDbHelper;
public class BootService extends Service {

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

    @Override
    public void onCreate() {
            super.onCreate();
            mDbHelper = new AppDatabase(this);
            mDbHelper.open();
    }

    @Override
    public void onStart(final Intent intent, final int startId) {
            super.onStart(intent, startId);
            LoadNotificationsFromDB();             
    }

    private void LoadNotificationsFromDB() {
            Cursor c = mDbHelper.getPendingNotifications();
            if (c.moveToFirst()) {
                    do {

                            int id = c.getInt(AppDatabase.ID_COLUMN);
                            String date = c.getString(AppDatabase.DATE_COLUMN);
                            addNotification(id, date);
                    } while (c.moveToNext());
            }
            c.close();
    }

    private void addNotification(int id, String date) {
            Intent intent = new Intent(BootService.this,    
                           NotificationBroadcastReceiver.class);

            PendingIntent mAlarmSender = PendingIntent.getBroadcast(
                            BootService.this, id, intent, 0);

            long alarmDate = Long.valueOf(date);

            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, date, mAlarmSender);
    }
}

然后创建一个接收器类:

Then Create a Receiver class:

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
            if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
                    Intent mServiceIntent = new Intent();
                    mServiceIntent.setAction("<package name>.BootService");
                    ComponentName service = context.startService(mServiceIntent);
                    if (null == service) {
                            // something really wrong here
                    }
            } 
    }
}

和下面的行添加到的Manifest.xml

    <receiver android:name=".BootReceiver" android:enabled="true">
            <intent-filter>
                    <action android:name ="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
    </receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 
精彩推荐