I am a beginner in the android world and I struggle at the moment with some concepts. One of them is the Handler object. I have reaserched this subject but I still don't have a full understanding of the subject. I might have some concepts confused. Here is a simple app I wrote that shows a progress bar on the screen.
我是android世界的初学者,我现在正在与一些概念斗争。其中一个是Handler对象。我已经重新研究了这个主题,但我仍然没有完全理解这个主题。我可能会混淆一些概念。这是我写的一个简单的应用程序,它在屏幕上显示了一个进度条。
package com.example.mjack.progressbarthread;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
Handler mHandler;
ProgressBar mProgressBar;
TextView mTextView;
Thread myThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mTextView = (TextView) findViewById(R.id.textview);
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
mProgressBar.setProgress(msg.arg1);
mTextView.setText("Progress.... " + (msg.arg1 +1) + " %");
if (msg.arg1 == 99) {
mTextView.setText("Done!!!");
}
}
};
myThread = new Thread(new MyRunnable());
myThread.start();
}
class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i<100; i++) {
Message message = mHandler.obtainMessage();
message.arg1 = i;
mHandler.sendMessage(message);
try {
myThread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
From what I understand so far (and please correct me if I'm wrong): 1.The main thread already has a Looper and Handler attached to it to be able to perform queued tasks (unlike new threads in which i have to prepare() a Looper and instantiate a Handler). 2.Only one Handler can be attached at a specific time to a thread (until it is replaced by a newly instantiated Handler object).
从我到目前为止的理解(如果我错了请纠正我):1。主线程已经连接了一个Looper和Handler,以便能够执行排队任务(不像我必须准备的新线程( )一个Looper并实例化一个Handler)。 2.只有一个Handler可以在特定时间附加到一个线程(直到它被一个新实例化的Handler对象替换)。
My question is: if I create a new Handler in the main thread and it is now attached to the thread and handles the messages from my background thread (through the overridden handleMessage method), what happens if I click on a button in that activity for example (if I had a button that does something in my layout) while the Handler is still in the process of handling messages from the background thread? what object handles the button click event (doesn't it have to go through a Handler considering there could be multiple events?)
我的问题是:如果我在主线程中创建一个新的Handler并且它现在附加到线程并处理来自我的后台线程的消息(通过重写的handleMessage方法),如果我点击该活动中的按钮会发生什么示例(如果我有一个按钮在我的布局中做某事)而Handler仍在处理来自后台线程的消息的过程中?什么对象处理按钮单击事件(考虑到可能有多个事件,它是否必须通过处理程序?)
I hope I have been clear enough. Thank you.
我希望我已经足够清楚了。谢谢。
1 个解决方案
#1
0
From reading the documentation and looking at this question, it looks like each handler will receive the message in the order that you instantiate them.
从阅读文档并查看此问题,看起来每个处理程序将按照您实例化它们的顺序接收消息。
#1
0
From reading the documentation and looking at this question, it looks like each handler will receive the message in the order that you instantiate them.
从阅读文档并查看此问题,看起来每个处理程序将按照您实例化它们的顺序接收消息。