可以从工作线程调用NoticationManager.notify()吗?

Jof*_*rey 43 notifications multithreading android android-notifications

我的问题更多的是关于什么是好的做法而不是可能的做法:

  • NoticationManager.notify()从工作线程调用是一件好事吗?
  • 系统是否在UI线程中执行它?

我总是试着记住,关于UI的东西应该在UI线程中执行,其余的在工作线程中执行,如Android doc关于进程和线程的建议:

此外,Andoid UI工具包不是线程安全的.因此,您不能从工作线程操纵UI - 您必须从UI线程对您的用户界面进行所有操作.因此,Android的单线程模型只有两个规则:

  • 不要阻止UI线程
  • 不要从UI线程外部访问Android UI工具包

然而,我对Android doc本身给出的一个例子感到惊讶(关于显示Notifications的进展),其中正在进行的通知进度直接从工作线程更新:

mNotifyManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Picture Download")
    .setContentText("Download in progress")
    .setSmallIcon(R.drawable.ic_notification);
// Start a lengthy operation in a background thread
new Thread(
    new Runnable() {
        @Override
        public void run() {
            int incr;
            // Do the "lengthy" operation 20 times
            for (incr = 0; incr <= 100; incr+=5) {
                    // Sets the progress indicator to a max value, the
                    // current completion percentage, and "determinate"
                    // state
                    mBuilder.setProgress(100, incr, false);
                    // Displays the progress bar for the first time.
                    mNotifyManager.notify(0, mBuilder.build());
                        // Sleeps the thread, simulating an operation
                        // that takes time
                        try {
                            // Sleep for 5 seconds
                            Thread.sleep(5*1000);
                        } catch (InterruptedException e) {
                            Log.d(TAG, "sleep failure");
                        }
            }
            // When the loop is finished, updates the notification
            mBuilder.setContentText("Download complete")
            // Removes the progress bar
                    .setProgress(0,0,false);
            mNotifyManager.notify(ID, mBuilder.build());
        }
    }
// Starts the thread by calling the run() method in its Runnable
).start();
Run Code Online (Sandbox Code Playgroud)

这就是为什么我想知道是否确实需要在主线程上运行它,或者系统是否需要处理它.

谢谢你的帮助!

Bre*_*age 79

Notification从工作线程更新a是可以接受的,因为Notification它不存在于应用程序的进程中,因此您不会直接更新其UI.通知在系统进程中维护,并且Notification通过RemoteViews(doc)更新UI ,允许操作由您自己以外的进程维护的视图层次结构.如果您查看Notification.Builder 此处的源代码,您可以看到它最终构建了一个RemoteViews.

如果您查看RemoteViews 此处的源代码,您会看到当您操作视图时,它实际上只是创建一个Action()对象并将其添加到要处理的队列中.一个ActionParcelable最终通过IPC发送到拥有该Notification视图的进程,在该进程中它可以解压缩值并在其自己的UI线程上更新所显示的视图.

我希望这澄清了为什么可以Notification从应用程序中的工作线程更新a .

  • 这正是我想要的,非常感谢你! (3认同)