3
我有以下代碼。這應該在logcat中以間隔發送消息,但不起作用。在stackoverflow上有很多類似的帖子,但我無法弄清楚這個問題。有什麼地方可以幫助我嗎?Android:爲什麼AlarmManager不工作?
<receiver android:name="BoopoohooAlarmReceiver"></receiver>
public void startAlarmManager(long interval){
Context context = getApplicationContext();
Intent intent = new Intent(context, BoopoohooAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
Log.i(DEBUG, "hollaa");
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);
}
public class BoopoohooAlarmReceiver extends BroadcastReceiver {
private final String DEBUG = "BoopoohooAlarmReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(DEBUG, "onReceive");
}
}
謝謝。