私の質問は、可能なことよりも、良い習慣とは何かについてです。
NoticationManager.notify()
ワーカースレッドから呼び出すのは良いことですか?- とにかくシステムはUIスレッドでそれを実行しますか?
プロセスとスレッドに関するAndroidのドキュメントで提案されているように、UIに関するものはUIスレッドで実行し、残りはワーカースレッドで実行する必要があることを常に念頭に置いています。
さらに、AndoidUIツールキットはスレッドセーフではありません。したがって、ワーカースレッドからUIを操作しないでください。UIスレッドからユーザーインターフェイスに対してすべての操作を行う必要があります。したがって、Androidのシングルスレッドモデルには2つのルールがあります。
- UIスレッドをブロックしないでください
- UIスレッドの外部からAndroidUIツールキットにアクセスしないでください
ただし、Androidドキュメント自体(通知での進行状況の表示について)の例には驚かされました。進行中の通知の進行状況は、ワーカースレッドから直接更新されました。
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();
そのため、実際にメインスレッドで実行する必要があるのか、それともシステムが処理するのか疑問に思っています。
ご協力いただきありがとうございます!