为什么我的 AlarmManager setRepeating 不重复?
我创建了一个 AlarmManager,它应该重复运行并启动服务。我的问题是它只运行一次。我不知道出了什么问题。现在,我已将 AlarmManager 的创建附加到按钮上。
这是 MenuActivity 类的一部分:
private void startUpdateService() {
Log.i(TAG, "Registering AlarmManager");
Intent service = new Intent(MenuActivity.this, UpdateService.class);
pendingIntent = PendingIntent.getService(MenuActivity.this, 0, service, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000, 10*1000, pendingIntent);
}
@Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.button_calendar:
Intent calendarIntent = new Intent(this, CalendarActivity.class);
if(Util.isOnline(this)){
startActivity(calendarIntent);
}else {
Toast.makeText(MenuActivity.this, R.string.toast_is_online, Toast.LENGTH_LONG).show();
}
break;
case R.id.button_settings:
startUpdateService();
Toast.makeText(MenuActivity.this, "Startet alarm", Toast.LENGTH_LONG).show();
break;
}
}
初始警报工作正常,但不会重复。 有人知道我做错了什么吗?
I've created an AlarmManager that should run repeatedly and start a service. My problem is it only runs once. I can't figure out what is wrong. For now I've attached creation of the AlarmManager to a button.
This is part of the MenuActivity class:
private void startUpdateService() {
Log.i(TAG, "Registering AlarmManager");
Intent service = new Intent(MenuActivity.this, UpdateService.class);
pendingIntent = PendingIntent.getService(MenuActivity.this, 0, service, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000, 10*1000, pendingIntent);
}
@Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.button_calendar:
Intent calendarIntent = new Intent(this, CalendarActivity.class);
if(Util.isOnline(this)){
startActivity(calendarIntent);
}else {
Toast.makeText(MenuActivity.this, R.string.toast_is_online, Toast.LENGTH_LONG).show();
}
break;
case R.id.button_settings:
startUpdateService();
Toast.makeText(MenuActivity.this, "Startet alarm", Toast.LENGTH_LONG).show();
break;
}
}
Initial alarm is working fine, but it won't repeat.
Anyone that knows what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我见过直接从警报运行服务的一些问题。我也有过。我所做的是用启动服务的广播接收器替换该服务。你为什么不也尝试一下呢? Advanced Android 对此进行了很好的解释,版本 1 可通过 CC 许可证获得。
I have seen few problems with running service straight from the alarm. I had it too. What i did, was replacing the service by the broadcast reciver which fires up the service. Why won't you try it too? It's explained well in Advanced Android, ver 1 is available on CC licence.