我有这个服务在后台做其他事情,并且必须始终运行。在其 onCreate 上,我设置了一个每天发送一次电子邮件的警报。服务器没有在前台运行,所以有时它会被系统杀死,然后再次创建,当这种情况发生时,它会再次创建警报,并在每次发生时发送另一封电子邮件。所以我的问题是,如果它已经设置为在系统中重复,有没有办法阻止它触发警报?或者在服务中是否有另一个“地方”我可以创建一次警报并防止它被重新创建?
public class ObserverService extends Service {
private String BABAS = "babas";
@Override
public void onCreate() {
setRecurringAlarm(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void setRecurringAlarm(Context context) {
Log.i(BABAS, "entrou no set alarme");
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getDefault());//getTimeZone("GMT"));
updateTime.set(Calendar.HOUR_OF_DAY, 00);
updateTime.set(Calendar.MINUTE, 00);
Intent downloader = new Intent("ALARME");//context, AlarmReceiver.class);
PendingIntent recurringDownload = PendingIntent.getBroadcast(context,
0, downloader, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, recurringDownload);
}
}