1.I have a problem about creating a handler in a child thread
like
public class Main4Activity extends Activity {
private TextView mTextView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text_view);
new Thread() {
@Override
public void run() {
Looper.prepare();
@SuppressLint("HandlerLeak") Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Toast.makeText(Main4Activity.this, "handler msg", Toast.LENGTH_SHORT).show();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
mTextView.setText("100");
}
};
handler.sendEmptyMessage(1);
Looper.loop();
}
}.start();
}
} The above code will crash.
Process: com.example.hellokai.kotlindemo, PID: 27485
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6986)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1074)
at android.view.View.requestLayout(View.java:19889)
at android.view.View.requestLayout(View.java:19889)
at android.view.View.requestLayout(View.java:19889)
at android.view.View.requestLayout(View.java:19889)
at android.support.constraint.ConstraintLayout.requestLayout(ConstraintLayout.java:1959)
at android.view.View.requestLayout(View.java:19889)
at android.widget.TextView.checkForRelayout(TextView.java:7369)
at android.widget.TextView.setText(TextView.java:4480)
at android.widget.TextView.setText(TextView.java:4337)
at android.widget.TextView.setText(TextView.java:4312)
at com.example.hellokai.kotlindemo.Main4Activity$1$1.handleMessage(Main4Activity.java:40)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at com.example.hellokai.kotlindemo.Main4Activity$1.run(Main4Activity.java:45)
2.I know to update ui in the main thread,handler creation in the main thread to create, and then send a message in the child thread to the handler can update Ui.
3.My question is what is the role of the handler created in the child thread? When do we need to do that? What is the use of the scene?
Hope someone can solve my confusion!
Your are updating ui from the background thread.
For Example you could send a message from a thread and update ui like
and then
Or If you just want a delay there is not need for a thread, looper and sleep as already noted in comments by pskink https://developer.android.com/reference/android/os/Handler.html
Yes your are right you can create the handler on the ui thread you can send message from the thread and update your ui
Handler is associated with a thread's looper. If you have a handler in ui thread its associated with it. In your case you have it inside a thread and hence handler is associated with that threads looper.
When you want to communicate from a backgroud thread to ui thread or from ui thread to background thread.