0
我的应用正在进行人类活动识别并通知用户它们是否不够活跃。我正在阅读始终运行的前台服务中的加速计数据。此刻,当用户正在睡觉时(例如,用户将睡眠时间间隔设置为晚上8点至早上8点),我试图启动和停止该服务,以便在指定的时间之后停止并重新开始服务。目前,下面的代码不起作用,我不知道为什么。有什么建议吗?在特定时间开始和停止服务
注:我知道,我将使代码重用莫名其妙的日历实例,这只是用于调试的目的,得到它的第一个工作日。
Date startSleepingHours = mUser.getStartSleepingHours();
Date stopSleepingHours = mUser.getStopSleepingHours();
//Create alarm manager
AlarmManager alarmMgr0 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, startSleepingHours.getHours());
cal.set(Calendar.MINUTE, startSleepingHours.getMinutes());
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
Intent startServiceIntent = new Intent(ActiveMinutesActivity.this, IntentBroadcastedReceiver.class);
Intent stopServiceIntent = new Intent(ActiveMinutesActivity.this, IntentBroadcastedReceiver.class);
stopServiceIntent.putExtra("command", "STOP_SERVICE");
PendingIntent stopServicePIntent = PendingIntent.getService(ActiveMinutesActivity.this, 0, stopServiceIntent, 0);
PendingIntent startServicePIntent = PendingIntent.getService(ActiveMinutesActivity.this, 1, startServiceIntent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60 * 1000, stopServicePIntent);
Calendar cal2 = new GregorianCalendar();
cal2.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal2.set(Calendar.HOUR_OF_DAY, stopSleepingHours.getHours());
cal2.set(Calendar.MINUTE, stopSleepingHours.getMinutes());
cal2.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal2.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal2.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal2.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), 60 * 1000, startServicePIntent);
我的接收器类:
public class IntentBroadcastedReceiver extends BroadcastReceiver {
public IntentBroadcastedReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getExtras() != null){
if(intent.getExtras().get("command").equals("STOP_SERVICE")){
context.stopService(new Intent(context, ActiveMinutesService.class));
}else {
context.startService(new Intent(context, ActiveMinutesService.class));
}
}
}
}
我知道它应该是评论,但我太newebie :) –
这并没有提供一个问题的答案。一旦你有足够的[声誉](https://stackoverflow.com/help/whats-reputation),你将可以[对任何帖子发表评论](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提问者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can- I-DO-代替)。 - [来自评论](/ review/low-quality-posts/17660611) – alexi2
感谢您的建议。是的,我已经在清单中定义了接收器。 –