Android AlarmManager不重复
我有以下问题:我在Android AlarmManager不重复,android,alarmmanager,android-alarms,android-intentservice,android-broadcastreceiver,Android,Alarmmanager,Android Alarms,Android Intentservice,Android Broadcastreceiver,我有以下问题:我在onCreate中注册了报警管理器,这是它第一次被禁用。但在我的延迟时间(1分钟)之后,应该会有另一次执行。但这不起作用。为什么? 我的代码: 报警接收器 public class AlarmReceiver extends WakefulBroadcastReceiver { private AlarmManager mAlarm; private PendingIntent mAlarmIntent; @Override public void onReceive(Co
onCreate
中注册了报警管理器,这是它第一次被禁用。但在我的延迟时间(1分钟)之后,应该会有另一次执行。但这不起作用。为什么?
我的代码:
报警接收器
public class AlarmReceiver extends WakefulBroadcastReceiver {
private AlarmManager mAlarm;
private PendingIntent mAlarmIntent;
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, PortalPullService.class);
String email = Helper.getEmail(context);
String pw = Helper.getPw(context);
service.putExtra("email", email);
service.putExtra("pw", pw);
startWakefulService(context, service);
}
public void setAlarm(Context context) {
mAlarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
mAlarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
int interval = R.integer.update_interval_in_mins * 60 * 1000;
mAlarm.setInexactRepeating(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
interval,
interval,
mAlarmIntent);
ComponentName reciever = new ComponentName(context, AlarmBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(reciever,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
public void cancelAlarm(Context context) {
if(mAlarm != null) {
mAlarm.cancel(mAlarmIntent);
}
ComponentName reciever = new ComponentName(context, AlarmBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(reciever,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
}
PullService
public class PortalPullService extends IntentService {
private static final String LOG_TAG = "PortalPullService";
public PortalPullService() {
super(LOG_TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
//TODO make request to ** check if new Infos are available, then send notification
Helper.sendNotification(this, "Test", "test"); /My test if this works
AlarmReceiver.completeWakefulIntent(intent);
}
}
通过new AlarmReceiver().setAlarm(此)注册接收器代码>
我不知道哪里是我的错
提前感谢;) 看看这一行:
int interval = R.integer.update_interval_in_mins * 60 * 1000;
R.integer.update\u interval\u(以分钟为单位)的值与您的想法不同。它是在构建期间分配的一个常量值,指向包含实际值的整数资源
如果要从资源中提取实际值,请执行以下操作:
int interval = context.getResources().getInteger(R.integer.update_interval_in_mins) * 60 * 1000;
而且,您的使用不正确。请参阅该文档。您将interval
传递给它,以代替第一次报警的时间(它不是间隔,而是绝对时间)
不要忘记,您可以记录值,以查看它们在使用时是什么。请看这一行:
int interval = R.integer.update_interval_in_mins * 60 * 1000;
R.integer.update\u interval\u(以分钟为单位)的值与您的想法不同。它是在构建期间分配的一个常量值,指向包含实际值的整数资源
如果要从资源中提取实际值,请执行以下操作:
int interval = context.getResources().getInteger(R.integer.update_interval_in_mins) * 60 * 1000;
而且,您的使用不正确。请参阅该文档。您将interval
传递给它,以代替第一次报警的时间(它不是间隔,而是绝对时间)
不要忘记,您可以记录值,以查看它们在使用时是什么。谢谢,我明天会尝试;)我理解的第一点是,我直接在ContextgetInteger
上搜索了一个方法,但没有找到这样的方法。所以我认为这可能是正确的值。不测试它是相当愚蠢的。对于第二点,我只是使用了他们在那里使用的代码,他们使用它的方式和我一样。。。(参考已过的实时唤醒示例)我阅读了文档,但我认为如果他们正在使用它,它应该是正确的。。。另外,代码是我写的;)真奇怪。在那个文档页面上,我认为“经过的实时唤醒”的例子是不正确的。稍后,RTC_唤醒的示例是正确的。查看它如何接收绝对日历时间。请记住,以毫秒为单位的触发时间的解释是基于第一个参数中确定的时钟。谢谢,我明天尝试;)我理解的第一点是,我直接在ContextgetInteger
上搜索了一个方法,但没有找到这样的方法。所以我认为这可能是正确的值。不测试它是相当愚蠢的。对于第二点,我只是使用了他们在那里使用的代码,他们使用它的方式和我一样。。。(参考已过的实时唤醒示例)我阅读了文档,但我认为如果他们正在使用它,它应该是正确的。。。另外,代码是我写的;)真奇怪。在那个文档页面上,我认为“经过的实时唤醒”的例子是不正确的。稍后,RTC_唤醒的示例是正确的。查看它如何接收绝对日历时间。请记住,以毫秒为单位的触发时间的解释基于第一个参数中确定的时钟。