del*_*116 11 android alarmmanager
我正在构建一个应用程序,它会在用户醒着的时候以特定的时间间隔触发通知.
我在服务中运行了一个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);
}
}
Run Code Online (Sandbox Code Playgroud)
.....和我的广播接收器实现在这里:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
对于如何实现这一点,我有点困难.
我正在考虑重做这段代码,以便在waketime时触发alarmManager并在sleepTime停止,所有在服务内部都是一个Timer,它以特定的时间间隔触发通知方法?有没有更好的方法来做这个?
任何输入将不胜感激.我一直试图在脑子里解决这个问题好几天了.
谢谢
编辑:@anyone遇到这个打算使用Timer进行日常通知:
当设备进入休眠状态时(即......用户将电话置于待机状态),运行时将暂停在服务内部运行的计时器.因此,使用计时器以特定时间间隔触发通知将无法在服务中正常工作,因为当Android暂停服务时,它还会暂停计时器,从而抛出间隔.
执行此操作的正确方法是使用具有待处理意图数组的AlarmManager在白天的特定时间设置警报.这样可以确保即使手机处于待机状态,通知(或当时您想要发生的任何事情)仍然会被执行.
我正在考虑修改这段代码,以便alarmManager在唤醒时触发并在睡眠时停止,而服务内部是一个以特定时间间隔触发通知方法的定时器?有更好的方法来做这件事吗?
在我看来,忘记思考“更好”的方法,这似乎是唯一的方法。使用一个计时器来控制(启用/禁用)另一个计时器并不奇怪,并且对我来说完全有意义。
| 归档时间: |
|
| 查看次数: |
13244 次 |
| 最近记录: |