在 Android 中使用 Handler 发送消息
Using Handler to sendMessage in Android
亲爱的。我正在研究 Android 开发,我一直在研究 Handlers/Loopers 和 MessageQueue。
根据文档,处理程序能够将 sendMessage 和 post(Runnable) 发送到另一个线程,因此我尝试了以下操作:
我有两个 java 文件,一个用于不同的 class:
public class MyThread extends Thread {
public MyThread(String argument) {
//save the arguments to some member attribute
}
@Override
public void run() {
//calculate something
String result = doSomething();
//now I need send this result to the MainActivity.
//Ive tried this
Bundle bundle = new Bundle();
bundle.putString("someKey", result);
Message msg = Message.obtain();
msg.what = 2;
msg.setData(bundle);
//I hope this could access the Main Thread message queue
new Handler(Looper.getMainLooper()).sendMessage(msg);
}
}
我的主要 Activity:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstance) {
//super and inflate the layout
//use findViewById to get some View
}
//I think this could access the same MessageQueue as the thread did
private Handler mHandler = new Hander() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 2) {
//I thought the sendMessage invoked inside the Thread
//would go here, where I can process the bundle and
//set some data to a View element and not only Thread change
}
}
}
}
由于阅读示例和文档我无法理解它的真正工作原理,因此我想要一个简单的解释,说明如何从线程(不知道 MainActivity)中获取一些数据显示到 Activity 或 Fragment.
内的视图中
谢谢
在你的例子中 main 是 Looper.getMainLooper()
,这意味着它会将消息发送到附加到 UI 线程的 Handler
,在你的例子中,MainActivity
是 UI 线程中的 运行,它在 Handler
的名称中有处理程序,因此消息将在 handleMessage
处接收。
您可以阅读更多关于 Looper, Communicating with the UI Thread, Handler
我认为基本上您在 UI 线程上从您的实现中创建了 2 个处理程序。这就是为什么在 MainActivity 中你没有被回调。
您可以尝试在 MyThread 中获取 mHandler 的引用,并在其上调用 sendMessage()。这样,您就可以为您的工作使用单个处理程序。
亲爱的。我正在研究 Android 开发,我一直在研究 Handlers/Loopers 和 MessageQueue。 根据文档,处理程序能够将 sendMessage 和 post(Runnable) 发送到另一个线程,因此我尝试了以下操作:
我有两个 java 文件,一个用于不同的 class:
public class MyThread extends Thread {
public MyThread(String argument) {
//save the arguments to some member attribute
}
@Override
public void run() {
//calculate something
String result = doSomething();
//now I need send this result to the MainActivity.
//Ive tried this
Bundle bundle = new Bundle();
bundle.putString("someKey", result);
Message msg = Message.obtain();
msg.what = 2;
msg.setData(bundle);
//I hope this could access the Main Thread message queue
new Handler(Looper.getMainLooper()).sendMessage(msg);
}
}
我的主要 Activity:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstance) {
//super and inflate the layout
//use findViewById to get some View
}
//I think this could access the same MessageQueue as the thread did
private Handler mHandler = new Hander() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 2) {
//I thought the sendMessage invoked inside the Thread
//would go here, where I can process the bundle and
//set some data to a View element and not only Thread change
}
}
}
}
由于阅读示例和文档我无法理解它的真正工作原理,因此我想要一个简单的解释,说明如何从线程(不知道 MainActivity)中获取一些数据显示到 Activity 或 Fragment.
内的视图中谢谢
在你的例子中 main 是 Looper.getMainLooper()
,这意味着它会将消息发送到附加到 UI 线程的 Handler
,在你的例子中,MainActivity
是 UI 线程中的 运行,它在 Handler
的名称中有处理程序,因此消息将在 handleMessage
处接收。
您可以阅读更多关于 Looper, Communicating with the UI Thread, Handler
我认为基本上您在 UI 线程上从您的实现中创建了 2 个处理程序。这就是为什么在 MainActivity 中你没有被回调。
您可以尝试在 MyThread 中获取 mHandler 的引用,并在其上调用 sendMessage()。这样,您就可以为您的工作使用单个处理程序。