我正在编写一个程序,定期启动意图以启动服务。为此,我决定使用AlarmManager。在活动中,我能够很容易地实现所需的功能,但是当我尝试在接收器中执行相同操作时,出现了一个错误,我无法解决它。
代码如下:
以下是该接收器的完整代码:
代码如下:
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);
}
}