我知道這是一個基本問題,但它仍然讓我瘋狂。我正在設置一個重複鬧鐘,但接收機從不會被呼叫。廣播接收器不叫
Intent intent = new Intent(NewSchedule.this, RepeatingAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(NewSchedule.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, calendar.getTimeInMillis(), 5 * 1000, sender);
Log.i("calendar",calendar.getTimeInMillis() + "");
Toast.makeText(NewSchedule.this, "repeating_scheduled", Toast.LENGTH_SHORT).show();
public class RepeatingAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "repeating_received", Toast.LENGTH_LONG).show();
}
}
<receiver android:name=".RepeatingAlarm" android:process=":remote" />
我正在測試我的手機。日曆日誌顯示確切的時間。我從來沒有在接收器類中得到Toast。
'sender = new Intent(appContext,RepeatingAlarm.class)'? –
試試這種方法:http://stackoverflow.com/a/7061752/801437 希望它有幫助。祝你好運 :) –