如何在处理程序线程

时间:2016-12-26 22:31:35

标签: android notifications android-looper android-handlerthread

我有一个包含四个RemoteView TextView的自定义通知。当onPause()被调用时,我想发布通知,然后在屏幕打开时每秒更新一次。当onResume()被调用时,我想取消通知。到目前为止,我已经设置好了,因此我有一个volatile布尔值,当调用onPause()时设置为true,并在调用onResume()时设置为false。我有一个方法启动HandlerThread然后检查volatile布尔值。如果布尔值为true,则应该更新通知,如果为false,则应该取消通知并关闭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");
    }

}

1 个答案:

答案 0 :(得分:1)

最多" android"这样做的方法是使用后台服务。

https://developer.android.com/guide/components/services.html

当你的应用程序进入后台(onPause)时,你要么启动服务,要么发一条消息说明这一点,而与onResume相反。

(您当前解决方案的问题是您的Activity可能随时在onStop / onDestroy上被杀死,但您仍然希望继续进行通知更新)

请注意,您使用MainThreads Looper创建了HandlerThread:

 timerNotificationHandler = new Handler(handlerThread.getLooper());

但是在你的else语句中你告诉这个looper退出:

 timerNotificationHandler.getLooper().quit();

我不太清楚这给了什么行为!但我觉得这是错误的 - 你不想让循环中的主线程停止,这意味着你的活动不会按预期工作(可能是你的问题)。

https://developer.android.com/reference/android/os/Looper.html#quit()

在您的其他声明中,您可能还想取消通知:

https://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)

timerNotificationManager.cancel(1234);