Android AlarmManager 不重复
Android AlarmManager not repeating
我有以下问题:我在 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(this);
注册
我不知道我错在哪里...
提前致谢;)
看看这一行:
int interval = R.integer.update_interval_in_mins * 60 * 1000;
R.integer.update_interval_in_mins 的值不是您想象的那样。它是在构建期间分配的常量值,指向包含实际值的整数资源。
如果您想从资源中提取实际值,请改为执行以下操作:
int interval = context.getResources().getInteger(R.integer.update_interval_in_mins) * 60 * 1000;
此外,您 setInexactRepeating() 的使用不正确。请参阅该文档。您将 interval
传递给它以代替第一个警报的时间(这不是间隔,而是绝对时间)。
别忘了您可以记录值以查看它们在使用时的状态。
我有以下问题:我在 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(this);
我不知道我错在哪里...
提前致谢;)
看看这一行:
int interval = R.integer.update_interval_in_mins * 60 * 1000;
R.integer.update_interval_in_mins 的值不是您想象的那样。它是在构建期间分配的常量值,指向包含实际值的整数资源。
如果您想从资源中提取实际值,请改为执行以下操作:
int interval = context.getResources().getInteger(R.integer.update_interval_in_mins) * 60 * 1000;
此外,您 setInexactRepeating() 的使用不正确。请参阅该文档。您将 interval
传递给它以代替第一个警报的时间(这不是间隔,而是绝对时间)。
别忘了您可以记录值以查看它们在使用时的状态。