我找到了問題的答案:private boolean service_started=false;
private PendingIntent mAlarmSender;
@Override
public void onReceive(Context context, Intent intent) {
if(!service_started){
//Create an IntentSender that will launch our service, to be scheduled
//with the alarm manager.
mAlarmSender = PendingIntent.getService(context,
0, new Intent(context, UpdateService.class), 0);
//We want the alarm to go off 30 secs from now.
long firstTime = SystemClock.elapsedRealtime();
//Schedule the alarm!
AlarmManager am = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime,30*1000, mAlarmSender);
service_started=true;
}
}
最後,我的問題是,我沒有按如下方式獲得上下文:(AlarmManager)getSystemService(ALARM_SERVICE);
更改為( AlarmManager ) 上下文。getSystemService ( 上下文。alarm_service ) ;