[英]AlarmManager responding at wrong time
对不起,如果以前回答过,但是我到处都是,但是没有找到合适的解决方案
我正在使用AlarmManager每天早上9点自动触发通知,但是当我尝试在模拟器上运行它时,它会立即执行,然后每半小时(准确地说是31-32分钟)执行一次,而不是每天早上9点才执行一次。
有什么想法吗? 帮助赞赏。
代码如下:
public class Home extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bsheet);
notificationAlert(savedInstanceState);
}
private void notificationAlert(Bundle savedInstanceState) {
AlarmManager manager;
PendingIntent pendingIntent;
Intent intent=new Intent(Home.this, Notify.class);
manager=(AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent=PendingIntent.getService(Home.this,
0, intent, 0);
GregorianCalendar gcal = new GregorianCalendar();
gcal.set(Calendar.HOUR_OF_DAY, 9);
gcal.set(Calendar.MINUTE, 0);
gcal.set(Calendar.SECOND, 0);
gcal.set(Calendar.MILLISECOND, 0);
long initTime = gcal.getTimeInMillis();
manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime,
24*60*60*1000, pendingIntent);
}
}
干杯,
编辑:我的意图是,一旦安装了该应用程序,它将在上午9点触发此警报。 我已将警报放在onCreate中,所以我不确定是否仅在每次启动应用程序时才创建警报,而当我隐藏应用程序时发生了一些奇怪的事情……再次感谢您的见解!
尝试这个 :
Calendar currentTime = Calendar.getInstance();
Calendar alarmTime = Calendar.getInstance();
currentTime.set(Calendar.HOUR_OF_DAY, 9;
currentTime.set(Calendar.MINUTE, 0);
currentTime.set(Calendar.SECOND, 0);
currentTime.set(Calendar.MILLISECOND, 0);
if (alarmTime.compareTo(currentTime) <= 0) {
// check for time
alarmTime.add(Calendar.DATE, 1);
}
Intent intent = new Intent(getBaseContext(), Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getBaseContext(), 1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime, 24*60*60*1000, pendingIntent);
如果initTime < System.currentTimeMillis()
警报管理器将立即触发
来自docs :
如果时间是过去的时间,则将立即触发警报,并根据过去的触发时间相对于重复间隔的时间来计算警报数。
根据您提供的代码, gcal.getTimeInMillis()
将返回对应于Today 9.00的 millisecods。 因此,如果您在这段时间之后尝试运行代码,则initTime将小于当前系统时间,这将触发AlarmManager的立即运行。
例如,要解决此问题,您可以在日历中添加一天,然后再通过gcal.getTimeInMillis()
如果已经过去),那么它将指向明天9.00,以便明天早上运行
更新1
尝试过这样的代码,它对我来说像预期的那样工作-每10秒触发一次服务:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initManager();
}
private void initManager() {
AlarmManager manager;
PendingIntent pendingIntent;
Intent intent = new Intent(this, NotifyService.class);
manager=(AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent=PendingIntent.getService(this, 0, intent, 0);
long initTime = System.currentTimeMillis() + 5000;
manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime,
10*1000, pendingIntent);
}
但是,您可以使用另一个选项:有AlarmManager.set()
方法,您可以像这样触发它
long runTime = /* your calendar time + 24 hours in millis*/
manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent);
因此它将在runTime
触发您的服务。 然后在该服务中,另一天用重新计算的runTime
调用相同的方法,因此它看起来像这样:
public MainActivity extends Activity{
protected onCreate(Bundle bundle){
....
initManager();
}
private initManager(){
...
long runTime = /* time of your first alarm/notification/whatever you develope */
Intent intent = new Intent(this, NotifyService.class);
pendingIntent=PendingIntent.getService(this, 0, intent, 0);
manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent);
}
}
和服务:
public NotifyService extends Service{
...
public int onStartCommand(...){
...
/* do stuff */
Intent intent = new Intent(getApplicationContext(), NotifyService.class);
pendingIntent=PendingIntent.getService(this, 0, intent, 0);
long runTime = /* recalculate it according to the next time of firing this service*/
manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent);
}
}
因此,每次服务触发时,您的服务都会在AlarmManager本身中注册一个意图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.