我正在编写一个程序,该程序会触发定期启动服务的 Intent ,为此我决定使用警报管理器,我能够很容易地做到这一点,但我在尝试时遇到错误在我无法弄清楚的接收器中进行。
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
告诉我 ALARM_SERVICE 无法解析为变量

这是我对该接收器的完整代码:

package com.testapp21.second.activities;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;

public class PhoneOnReceiver extends BroadcastReceiver {
private PendingIntent mAlarmSender;

@Override
public void onReceive(Context context, Intent intent) {
    mAlarmSender = PendingIntent.getService(context,
              0, new Intent(context, StatsCheckerService.class), 0);

    // We want the alarm to go off 30 seconds from now.
      long firstTime = SystemClock.elapsedRealtime();

      // Schedule the alarm!
      AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
      am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                      firstTime, 30*1000, mAlarmSender);

}
}

最佳答案

尝试

AlarmManager am = (AlarmManager)context.getSystemService(Service.ALARM_SERVICE);

关于android - 使用广播接收器在 Android 中启动 AlarmManager?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9603128/

10-11 15:08