如何确保AlarmManager能够在电话重启和应用程序终止后幸免于难

时间:2012-07-17 17:19:37

标签: android alarmmanager

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

为此,我使用AlarmManager来安排警报,然后使用它来生成通知

但我面临的问题是,如果用户“强制关闭”应用程序,则会重置警报,因此我不会收到任何通知。这是正常的行为吗?这有什么办法吗?

此外,如果手机重新启动,警报也会被取消。我该如何控制?我是否需要收听电话重启事件并再次安排警报?这是唯一的选择吗?

2 个答案:

答案 0 :(得分:2)

我的数据库中有一个表,可以在重新启动时存储所有待处理的警报。如果手机重新启动,我有一个重置警报的服务等级。

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

然后创建一个Receiver类:

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" />

答案 1 :(得分:1)

您将需要设置与此类似的内容以挂钩启动完成的意图。

<receiver android:name="MyStartupIntentReceiver">
<intent-filter>
<action
  android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>

一旦你有了,你将需要开始服务或在

做一些事情
public void onReceive(Context context, Intent intent) {
    Intent serviceIntent = new Intent();
    serviceIntent.setAction("com.yourapp.yourservice");
    context.startService(serviceIntent);
}