這是我第一次使用AlarmReciever
。這是服務的一部分:AlarmManager在不同的API的不同時間後觸發
//Service
this.context=this;
Intent alarm = new Intent(this.context,AlarmReceiver.class);
boolean alarmRunning =(PendingIntent.getBroadcast(this.context,0, alarm,PendingIntent.FLAG_NO_CREATE) != null);
if(alarmRunning == false){
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.context,0,alarm,0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),15000,pendingIntent);
}
我認爲在15000手段alarmManager.setRepeating
15秒。在我的S4 mini上用API 19是對的。但隨着我的S7與API 23警報每分鐘都會發生。有誰知道這個問題嗎?
這裏是我的日誌:
08-15 10:54:15.949 11495-11522/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 10:55:15.619 11495-12452/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 10:56:15.619 11495-13407/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 10:57:15.629 11495-14288/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 10:58:15.629 11495-15193/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 11:00:00.039 11495-16907/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 11:00:15.599 11495-17149/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 11:01:15.619 11495-18085/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 11:02:15.659 11495-19040/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 11:03:58.049 11495-19601/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 11:06:11.419 11495-19878/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 11:11:11.349 11495-24547/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 11:16:11.449 11495-29123/com.example.entwicklung1.designtestapp D/BackgroundRunning::
當我看到,現在觸發每隔幾分鐘。
非常感謝。那麼我應該使用上面的代碼而不是alarmManager.setRepeating ...? – Koss
是的,只需複製這個確切的代碼,等待我修改它,這樣你就可以複製並不需要修改你的代碼。 –
我的榮幸,請接受答案並投票表決,如果它的工作。 :)) –