在这段代码中,我遇到了一个问题,我不确定如何为setRecurringAlarm()设置上下文。在这个代码示例中,我将 此 ,但我不确定这是否正确。
public class AlarmReceiver extends BroadcastReceiver {
private static final int INTERVAL = 15*1000; // check every 15 seconds
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction() != null) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.v(TAG, "BroadcastReceiver - Received Boot Completed; Set Alarm");
setRecurringAlarm(this);
}else{
Log.v(TAG, "BroadcastReceiver - Received " + intent.getAction());
}
}else{
Log.v(TAG, "BroadcastReceiver - Received AlarmService");
}
}
private void setRecurringAlarm(Context context) {
Intent receiver = new Intent(context, AlarmReceiver.class);
PendingIntent recurringDownload = PendingIntent.getBroadcast(context, 0, receiver, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), INTERVAL, recurringDownload);
}
}
答案 0 :(得分:0)
您已在onReceive(Context context, Intent intent)
中获得上下文,请使用它。