警报管理器未决意图未触发服务
Alarm Manager Pending Intent not firing Service
我有一个 alarm
我想每 10 分钟触发一次,但它没有触发。这是我在 MainActivity
中设置 AlarmService
的地方。是的,我已经声明了 WAKE_LOCK
权限。
Calendar cal2 = Calendar.getInstance();
cal2.add(Calendar.MINUTE, 1);
PendingIntent getSqlUpdatesTimer = PendingIntent.getService(this, 0, new Intent(AlarmService.PULL_SQLUPDATES_ACTION, null, this, AlarmService.class), 0);
alarmManager.cancel(getSqlUpdatesTimer);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), 600000, getSqlUpdatesTimer);
这是我的报警服务:
public class AlarmService extends Service {
public static final String PULL_SQLUPDATES_ACTION = "com.mycompany.AlarmService.PULL_SQLUPDATES";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
if (PULL_SQLUPDATES_ACTION.equals(intent.getAction())) {
PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wl.acquire();
GetSQLUpdates retrieveSQLUpdates = new GetSQLUpdates(null);
retrieveSQLUpdates.execute("sp_A_Get_SQLUpdates", "sp_A_Put_SQLUpdates");
wl.release();
}
}
return START_STICKY;
}
}
我错过了什么?
我无法以这种方式启动它,所以我使用了 BroadcastReceiver
而不是来自此答案的服务:
Alarm Manager Example:
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
//set the alarms 5 minutes apart so they don't conflict.
cal.add(Calendar.MILLISECOND,GlobalVars.timeToCheckForKillMe);
//getSqlUpdatesTimer = PendingIntent.getService(this, 0, new Intent(AlarmService.PULL_SQLUPDATES_ACTION, null, this, AlarmService.class), 0);
Intent i = new Intent(this, AlarmBroadcastReceiver.class);
getSqlUpdatesTimer = PendingIntent.getBroadcast(this, 0, i, 0);
alarmManager.cancel(getSqlUpdatesTimer);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), GlobalVars.timeToCheckForKillMe, getSqlUpdatesTimer);
和class:
public class AlarmBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
if (intent != null) {
PowerManager pm = (PowerManager)context.getSystemService(context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wl.acquire();
EDiaryDataSync.GetSQLUpdates retrieveSQLUpdates = new EDiaryDataSync.GetSQLUpdates(null);
retrieveSQLUpdates.execute("sp_A_Get_SQLUpdates", "sp_A_Put_SQLUpdates");
wl.release();
}
}
}
我有一个 alarm
我想每 10 分钟触发一次,但它没有触发。这是我在 MainActivity
中设置 AlarmService
的地方。是的,我已经声明了 WAKE_LOCK
权限。
Calendar cal2 = Calendar.getInstance();
cal2.add(Calendar.MINUTE, 1);
PendingIntent getSqlUpdatesTimer = PendingIntent.getService(this, 0, new Intent(AlarmService.PULL_SQLUPDATES_ACTION, null, this, AlarmService.class), 0);
alarmManager.cancel(getSqlUpdatesTimer);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), 600000, getSqlUpdatesTimer);
这是我的报警服务:
public class AlarmService extends Service {
public static final String PULL_SQLUPDATES_ACTION = "com.mycompany.AlarmService.PULL_SQLUPDATES";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
if (PULL_SQLUPDATES_ACTION.equals(intent.getAction())) {
PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wl.acquire();
GetSQLUpdates retrieveSQLUpdates = new GetSQLUpdates(null);
retrieveSQLUpdates.execute("sp_A_Get_SQLUpdates", "sp_A_Put_SQLUpdates");
wl.release();
}
}
return START_STICKY;
}
}
我错过了什么?
我无法以这种方式启动它,所以我使用了 BroadcastReceiver
而不是来自此答案的服务:
Alarm Manager Example:
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
//set the alarms 5 minutes apart so they don't conflict.
cal.add(Calendar.MILLISECOND,GlobalVars.timeToCheckForKillMe);
//getSqlUpdatesTimer = PendingIntent.getService(this, 0, new Intent(AlarmService.PULL_SQLUPDATES_ACTION, null, this, AlarmService.class), 0);
Intent i = new Intent(this, AlarmBroadcastReceiver.class);
getSqlUpdatesTimer = PendingIntent.getBroadcast(this, 0, i, 0);
alarmManager.cancel(getSqlUpdatesTimer);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), GlobalVars.timeToCheckForKillMe, getSqlUpdatesTimer);
和class:
public class AlarmBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
if (intent != null) {
PowerManager pm = (PowerManager)context.getSystemService(context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wl.acquire();
EDiaryDataSync.GetSQLUpdates retrieveSQLUpdates = new EDiaryDataSync.GetSQLUpdates(null);
retrieveSQLUpdates.execute("sp_A_Get_SQLUpdates", "sp_A_Put_SQLUpdates");
wl.release();
}
}
}