你好,這是我的第一個關於堆棧溢出的問題。我正在編寫一個Android應用程序,它是一個多計時器。我設置的方式是,當onStop()被調用時,我將取消所有的定時器,並在AlarmManager中啓動四個待定目標,繼續倒計時。如果用戶返回應用程序,AlarmManager中的定時器將被取消,並且會創建新的定時器繼續倒計時。我使用sharedpreferences來存儲所有的值。多重報警brodcastreceiver android
我很確定問題出在我的BroadcastReceiver類中。發生什麼事是我啓動應用程序,我啓動多個定時器,點擊後退按鈕(調用onStop),然後返回到應用程序並停止並重置所有定時器。然後再按一次(再次調用onStop),並且BroadcastReceiver觸發四次。儘管因爲定時器全部被重置,所以它不應該被解僱。
同樣,我認爲問題出在OnReceive上,因爲我沒有爲四個pendingIntents設置它,因爲我無法弄清楚如何。這就是我的實際問題。我可以在一個BroadcastReceiver類中設置四個pendingIntents嗎?如果是這樣,我該怎麼做?
我還想設置它,這樣如果鬧鐘已經播放,並且鬧鐘管理器中的另一個鬧鐘結束了,它不會再次開始播放鬧鐘聲音,而只是更改已存在的通知上的文本。
P.S.我對Android和編程一般都很陌生。我很抱歉,如果這是一個愚蠢的問題,並且如果我的帖子的格式不是很好。
設置AlarmManager代碼:
public void setAlarmManager() {
long wakeUpTime = timerPreferences.getStartTime() + millisToCount;
long wakeUpTimeTwo = timerPreferences.getStartTimeTwo() + millisToCountTwo;
long wakeUpTimeThree = timerPreferences.getStartTimeThree() + millisToCountThree;
long wakeUpTimeFour = timerPreferences.getStartTimeFour() + millisToCountFour;
Log.v(TAG, "Millis For AM " + millisToCount);
Log.v(TAG, "Millis Two For AM " + millisToCountTwo);
Log.v(TAG, "Millis Three For AM " + millisToCountThree);
Log.v(TAG, "Millis Four For AM " + millisToCountFour);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
if (millisToCount > 0) {
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
am.setAlarmClock(new AlarmManager.AlarmClockInfo(wakeUpTime, sender), sender);
} else {
am.set(AlarmManager.RTC_WAKEUP, wakeUpTime, sender);
}
Log.v(TAG, "Alarm Manager Set");
}
if (millisToCountTwo > 0) {
PendingIntent senderTwo = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
am.setAlarmClock(new AlarmManager.AlarmClockInfo(wakeUpTimeTwo, senderTwo), senderTwo);
} else {
am.set(AlarmManager.RTC_WAKEUP, wakeUpTimeTwo, senderTwo);
}
Log.v(TAG, "Alarm Manager Two Set");
}
if (millisToCountThree > 0) {
PendingIntent senderThree = PendingIntent.getBroadcast(this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
am.setAlarmClock(new AlarmManager.AlarmClockInfo(wakeUpTimeThree, senderThree), senderThree);
} else {
am.set(AlarmManager.RTC_WAKEUP, wakeUpTimeThree, senderThree);
}
Log.v(TAG, "Alarm Manager Three Set");
}
if (millisToCountFour > 0) {
PendingIntent senderFour = PendingIntent.getBroadcast(this, 3, intent, PendingIntent.FLAG_CANCEL_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
am.setAlarmClock(new AlarmManager.AlarmClockInfo(wakeUpTimeFour, senderFour), senderFour);
} else {
am.set(AlarmManager.RTC_WAKEUP, wakeUpTimeFour, senderFour);
}
Log.v(TAG, "Alarm Manager Four Set");
}
}
代碼去除AlarmManager:
public void removeAlarmManager() {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent senderTwo = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent senderThree = PendingIntent.getBroadcast(this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent senderFour = PendingIntent.getBroadcast(this, 3, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.cancel(sender);
am.cancel(senderTwo);
am.cancel(senderThree);
am.cancel(senderFour);
}
的BroadcastReceiver類報警:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder timerNotificationBuilder = new NotificationCompat.Builder(context);
Uri notification = Uri.parse("android.resource://" + "com.example.iinewmanii.professionalkitchentimer" + "/" + R.raw.alarm_clock_short);
timerNotificationBuilder.setSound(notification)
.setPriority(2)
.setColor(Color.rgb(239, 82, 79))
.setContentTitle("Professional Kitchen timer")
.setAutoCancel(true)
.setContentText("Timer 1 Has Finished")
.setSmallIcon(R.mipmap.ic_warning_white_24dp)
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
timerNotificationBuilder.setCategory(CATEGORY_ALARM);
}
Notification timerNotification = timerNotificationBuilder.build();
timerNotification.flags |= Notification.FLAG_INSISTENT;
NotificationManager timerNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
timerNotificationManager.notify(0, timerNotification);
}
}