0

我有一个调度程序类型的应用程序,它在早上和晚上的指定时间向用户发送通知。我已经使用警报管理器完成了这项工作。在模拟器上,就这一点而言,应用程序工作得很好。但是在我的设备中,我使用“高级任务杀手”应用程序来杀死正在运行的应用程序并释放设备的内存。我注意到在终止此应用程序时,通知不会在预定时间显示。这绝对看起来合乎逻辑,但根本不是我想要的。即使进程被终止,我也希望在预定时间显示通知。有没有办法这样做?

我想一些代码可能有助于解决我的问题:

我的主要活动中有这个

public void setAlarm(){
    Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    am.cancel(pendingIntent);
    Cursor tempcursor = db.getAlarmTime();
    tempcursor.moveToFirst();
    int hour = tempcursor.getInt(tempcursor.getColumnIndex("Hour"));
    int minute = tempcursor.getInt(tempcursor.getColumnIndex("Minute"));
    tempcursor.close();
    db.close();
    GregorianCalendar alarmtime = new GregorianCalendar();
    alarmtime.set(GregorianCalendar.HOUR_OF_DAY, hour);
    alarmtime.set(GregorianCalendar.MINUTE, minute);
    alarmtime.set(GregorianCalendar.SECOND, 0);
    alarmtime.set(GregorianCalendar.MILLISECOND, 0);
    if(alarmtime.before(new GregorianCalendar()))alarmtime.add(GregorianCalendar.DAY_OF_MONTH, 1);
    am.set(AlarmManager.RTC_WAKEUP, alarmtime.getTimeInMillis(), pendingIntent);

}

这在我扩展 BroadcastReceiver 的 AlarmReceiver 类中:

public void onReceive(Context context, Intent intent) {
    notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent i = new Intent(context, Schedule_Today.class);
    PendingIntent pi = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pi2 = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + AlarmManager.INTERVAL_DAY, pi2); //Setting another notification after 1 day as soon this notification broadcast is received.


        CharSequence from = "Scheduler_3";
        CharSequence message = "Test Notification";
        notification = new Notification(R.drawable.ic_launcher, "Attention",System.currentTimeMillis());
        notification.setLatestEventInfo(context, from, message, pi);


    notificationManager.notify(1, notification);
}
4

1 回答 1

1

I want to prevent the application from being killed or at least a module of it running that can broadcast the alarmmanager at the required time so that the user receives notifications

This is not strictly possible, except by making your own version of Android in your own ROM mod.

On Android 2.1 and earlier, third-party task managers, like "Advanced Task Killer", had the ability to "force stop" an application. On Android 2.2 and higher, that ability was reserved for the OS itself, and is available to users via the "Force Stop" button on the app's screen in the list of applications in Settings.

When an app is "force stopped", among other things, all scheduled alarms are removed. In addition, on Android 3.1+, nothing of that app will ever run again, until the user manually launches one of your activities (or something else manually runs one of your components).

You are welcome to write two applications, one that is the main app and the other than ensures that, if the first one appears to have been force-stopped, the alarms are rescheduled. However, there is nothing stopping the user from force-stopping both of those applications.

Also, bear in mind that some devices, like the SONY Xperia Z, block _WAKEUP alarms in general, if the user has activated "STAMINA Mode". See this blog post for more about this.

Hence, I recommend that you redesign your application to take into account that your alarms are not guaranteed to run at all, let alone at the time you expect.

于 2013-06-19T13:26:32.500 回答