我已将AlarmManager设置为重复调用服务以更新数据库。但是似乎此AlarmManager从未调用我的服务。 我将system.out投入使用,但从未在输出中打印出来。
我的MainActivity中有这段代码:
Intent intent = new Intent(getApplicationContext(), ServiceAreaAlarmReceiver.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, ServiceAreaAlarmReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
long firstMillis = System.currentTimeMillis();
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstMillis + 60000, 60000, pIntent);
这是我的清单:
<receiver
android:name=".service.ServiceAreaAlarmReceiver" />
<service
android:name=".service.UpdateDatabaseService"
android:exported="false" />
这是我的服务:
public class ServiceAreaAlarmReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 12345;
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("got here");
context.startService(new Intent(context, UpdateDatabaseService.class));
}
}