【发布时间】:2013-03-17 02:09:34
【问题描述】:
我正在构建一个应用程序,该应用程序将在用户醒着的特定时间间隔触发通知。
我有一个在服务内部运行的 alarmManager。该服务通过单击主要活动的按钮显式启动,并让 alarmManager 在特定时间反转期间执行通知。我将如何在一天中的某些时间停止通知?例如,我不希望在用户睡觉时触发这些通知。
我当前以用户设置的间隔触发通知的代码如下(已删除导入......这已经足够长了):
public class FartSmackinChunks extends Service {
public Notification scheduleNotification;
public AlarmManager alarmScheduleManager;
public PendingIntent alarmScheduleIntent;
private Boolean autoUpdateBoolean = true;
private int intervalsGoneByInt = 0;
private Notification notification;
public static final int NOTIFICATION_ID = 1;
@Override
public void onCreate() {
// TODO: Actions to perform when service is created.
int icon = R.drawable.icon;
String tickerText = "INTERVAL FIRED";
long when = System.currentTimeMillis();
scheduleNotification = new Notification(icon, tickerText, when);
alarmScheduleManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
String ALARM_ACTION;
ALARM_ACTION = ScheduleAlarmReceiver.ACTION_REFRESH_SCHEDULE_ALARM;
Intent intentToFire = new Intent(ALARM_ACTION);
alarmScheduleIntent = PendingIntent.getBroadcast(this, 0, intentToFire,
0);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
SharedPreferences mySharedPreferences =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean autoUpdateBoolean =
mySharedPreferences.getBoolean("storedAutoUpdateBoolean", false);
String updateFreq =
mySharedPreferences.getString("storedInitialAverageTimeInterval", "00:00:00");
SimpleDateFormat dfInterval = new SimpleDateFormat("hh:mm:ss");
Date intervalTimeAsDateObject = null;
long updateFreqMilliLong;
try {
intervalTimeAsDateObject = dfInterval.parse(updateFreq);
} catch (ParseException e) {
e.printStackTrace();
}
updateFreqMilliLong = intervalTimeAsDateObject.getTime() - 18000000;
if (autoUpdateBoolean) {
int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
long timetoRefresh = SystemClock.elapsedRealtime() +
updateFreqMilliLong;
alarmScheduleManager.setInexactRepeating(alarmType,
timetoRefresh, updateFreqMilliLong, alarmScheduleIntent);
notifications();
} else alarmScheduleManager.cancel(alarmScheduleIntent);
return Service.START_NOT_STICKY;
};
private void notifications() {
**notification stuff in here***
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Replace with service binding implementation.
return null;
}
@Override
public void onDestroy() {
this.alarmScheduleManager.cancel(alarmScheduleIntent);
}
}
.....还有我的广播接收器实现:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class ScheduleAlarmReceiver extends BroadcastReceiver {
public static final String ACTION_REFRESH_SCHEDULE_ALARM
= "com.application.ACTION_REFRESH_SCHEDULE_ALARM";
@Override
public void onReceive(Context context, Intent intent) {
Intent startIntent = new Intent(context, SmokerReducerService.class);
context.startService(startIntent);
}
}
我在思考应该如何实现这一点时遇到了一些困难。
我正在考虑重新编写此代码,以便在唤醒时间触发警报管理器并在睡眠时间停止,而在服务内部是一个以特定时间间隔触发通知方法的计时器?有没有更好的方法来做到这一点?
任何意见将不胜感激。这几天我一直在努力解决这个问题。
谢谢
编辑: @任何遇到这种情况并打算使用计时器进行每日通知的人:
当设备进入睡眠状态(即……用户将手机置于待机状态)时,运行在服务内部的计时器将被运行时暂停。因此,使用 Timer 在特定时间间隔触发通知将无法在服务中正常工作,因为当 Android 暂停服务时,它也会暂停计时器,从而引发间隔。
执行此操作的正确方法是使用具有一系列待处理意图的 AlarmManager 在一天中的特定时间设置警报。这样可以确保即使手机处于待机状态,通知(或您当时想要发生的任何事情)仍然会被执行。
【问题讨论】:
-
mySharedPreferences.getBoolean丢失 - 需要// TODO: Actions to perform when service is created吗? (至少有问题 - 另一个 TODO 还可以)另外为什么要使用(长期运行的)服务? -
@Mr_and_Mrs_D - 我最终使用了带有一系列待处理意图的 AlarmManager 类来执行此操作。
标签: android alarmmanager