我有一个具有四个远程视图文本视图的自定义通知。当onpause()被调用时,我想发布通知,然后在屏幕打开时每秒更新它。当onResume()被调用时,我想取消通知。到目前为止,我已经设置了它,以便我有一个挥发性的布尔值,当onpause()被调用并设置为false时,当onResume()被调用时。我有一种启动处理程序的方法,然后检查挥发性布尔值。如果布尔值为真,则应该更新通知,如果是错误的,则应该取消通知并关闭处理程序。
,就启动通知并每秒更新通知,一切似乎都可以正常工作。但是,当称为onResume()时,它不会停止通知。
我还没有编码的内容上的屏幕,因为我试图弄清楚如何关闭通知和处理程序。
这是我到目前为止的代码:
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");
}
}
做到这一点的最" android"方法是使用背景服务。
https://developer.android.com/guide/components/services.html
当您的应用程序移至背景(Onpause)时,您要么启动服务,要么向说明这一点的俗语发布消息,而与on Resume相反。
(您当前的解决方案的问题是您的活动可能在脚步化/ondestroy时被杀死,但您仍然需要进行通知更新)
请注意,您使用mainthreads looper创建处理程序:
timerNotificationHandler = new Handler(handlerThread.getLooper());
但是在您的其他语句中,您告诉这个循环器要退出:
timerNotificationHandler.getLooper().quit();
我不太确定这会带来什么行为!但是我觉得这是错误的 - 您不想阻止Mainthreads Looper循环,这意味着您的活动无法正常工作(这可能是您的问题)。
https://developer.android.com/reference/android/os/looper.html#quit()
在您的其他声明中,您可能还需要取消您的通知:
https://developer.android.com/reference/android/App/notificationmanager.html#cancel(int)
timerNotificationManager.cancel(1234);