我有一个具有四个 RemoteView TextViews 的自定义通知。当 onPause() 被调用时,我想发布通知,然后在屏幕打开时每秒更新一次。当 onResume() 被调用时,我想取消通知。到目前为止,我已经设置好了,所以我有一个 volatile 布尔值,当 onPause() 被调用时设置为 true,当 onResume() 被调用时设置为 false。我有一个启动 HandlerThread 然后检查 volatile 布尔值的方法。如果布尔值为真,则应该更新通知,如果为假,则应该取消通知并关闭 HandlerThread。
就开始通知并每秒更新一次而言,一切似乎都运行良好。但是,当 onResume() 被调用时,它不会停止通知。
我还没有编码内容的屏幕,因为在我继续之前我试图弄清楚如何关闭通知和 HandlerThread。
到目前为止,这是我的代码:
void setUpTimerNotif() {
handlerThread = new HandlerThread("TimerNotificationHandler", Process.THREAD_PRIORITY_BACKGROUND);
handlerThread.start();
timerNotificationHandler = new Handler(handlerThread.getLooper());
timerNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent timerNotifIntent = new Intent(MainActivity.this, MainActivity.class);
timerNotifIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent pendingTimerNotifIntent = PendingIntent.getActivity(MainActivity.this,
1234, timerNotifIntent, PendingIntent.FLAG_UPDATE_CURRENT);
final RemoteViews remoteTimerViews = new RemoteViews(getPackageName(), R.layout.timer_notification);
final NotificationCompat.Builder timerNotificationBuilder = new NotificationCompat.Builder(MainActivity.this);
if (timerNotificationRunning) {
timerNotificationHandler.postDelayed(
new Runnable() {
@Override
public void run() {
long timerNotifTimeOne = wakeUpTime - System.currentTimeMillis();
long timerNotifTimeTwo = wakeUpTimeTwo - System.currentTimeMillis();
long timerNotifTimeThree = wakeUpTimeThree - System.currentTimeMillis();
long timerNotifTimeFour = wakeUpTimeFour - System.currentTimeMillis();
String timeOne = timerFormat(timerNotifTimeOne);
String timeTwo = timerFormat(timerNotifTimeTwo);
String timeThree = timerFormat(timerNotifTimeThree);
String timeFour = timerFormat(timerNotifTimeFour);
String hmsOne = ("Timer 1: " + timeOne);
String hmsTwo = ("Timer 2: " + timeTwo);
String hmsThree = ("Timer 3: " + timeThree);
String hmsfour = ("Timer 4: " + timeFour);
remoteTimerViews.setTextViewText(R.id.timer_one_notification_view, hmsOne);
remoteTimerViews.setTextViewText(R.id.timer_two_notification_view, hmsTwo);
remoteTimerViews.setTextViewText(R.id.timer_three_notification_view, hmsThree);
remoteTimerViews.setTextViewText(R.id.timer_four_notification_view, hmsfour);
timerNotificationBuilder.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(false)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingTimerNotifIntent)
.setCustomBigContentView(remoteTimerViews)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
timerNotificationManager.notify(1234, timerNotificationBuilder.build());
timerNotificationHandler.post(this);
}
}, 1000
);
} else {
timerNotificationManager.cancelAll();
if (timerNotificationHandler != null) {
timerNotificationHandler.removeCallbacksAndMessages(null);
}
timerNotificationHandler.getLooper().quit();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
handlerThread.quitSafely();
} else {
handlerThread.quit();
/*try {
handlerThread.join();
handlerThread = null;
timerNotificationHandler = null;
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
Log.v(TAG, "Timer Notification Cancelled");
}
}