我試圖設置鬧鐘管理器。下面是我做什麼:Android應用程序關閉時未觸發Android鬧鐘管理器
private static AlarmManager alarmManager;
private BroadcastReceiver alarmReceiver;
private PendingIntent alarmIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Hello", Toast.LENGTH_LONG).show();
WakeLocker.acquire(context);
}
};
registerReceiver(alarmReceiver, new IntentFilter("com.alarm.wakemeup"));
alarmIntent = PendingIntent.getBroadcast(this, 0, new Intent("com.alarm.wakemeup"),0);
alarmManager = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));
SetAlarm(getApplicationContext());
}
public void SetAlarm(Context context)
{
alarmManager.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() +
5000, alarmIntent);
}
這工作得很好,當我剛剛鎖定屏幕:手機喚醒和吐司5秒後寫在屏幕上。但是,當我顯式退出應用程序時,警報管理器不會被觸發。有什麼建議麼?
謝謝
我認爲它是由於它在onCreate中註冊。當你退出應用程序時,它的實例是不會被銷燬的?在清單中註冊。 – 2014-08-27 11:44:08
@Dhruti你是對的,那個作品謝謝你! – yrazlik 2014-08-27 11:59:03
偉大的:)快樂編碼。 – 2014-08-27 12:01:08