- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试掌握 Android 中的多线程。我的目标是将数据从主线程发送到工作线程。
我在其 onCreate
中有一个包含以下代码的主要 Activity -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ThreadOne threadOne = new ThreadOne();
threadOne.start();
threadOneLooper = threadOne.getLooper();
Message msg = new Message();
msg.obj = "message.";
Log.i("id--(threadOneLooper)", String.valueOf(threadOneLooper.getThread().getId()));
Log.i("id--(Main)", String.valueOf(getMainLooper().getThread().getId()));
TestHandler mHandler = new TestHandler(threadOneLooper);
mHandler.handleMessage(msg);
}
如您所见,我在 threadOneLooper
中存储了工作线程循环程序的引用,然后记录了其关联线程的 ID。同时我也打印了主线程的id。
以下是我的线程的代码 -
public class ThreadOne extends Thread{
@Override
public void run() {
if (Looper.myLooper() == null)
Looper.prepare();
Log.i("ThreadOne", "run()");
// Just making sure all approaches give the same result
Log.i("Id--Thread id 1", String.valueOf(getLooper().getThread().getId()));
Log.i("Id--Thread id 2", String.valueOf(getId()));
Looper.loop();
}
Looper getLooper() {
if (Looper.myLooper() != null)
return Looper.myLooper();
else
return null;
}
下面是我的处理程序的代码-
public class TestHandler extends Handler {
TestHandler(Looper myLooper) {
super(myLooper);
}
public void handleMessage(Message msg) {
String txt = (String) msg.obj;
Log.i("Handler", txt);
Log.i("Id--(Handler)", String.valueOf(getLooper().getThread().getId()));
}
}
现在,问题是我假设 Log.i("Id--(Handler)", String.valueOf(getLooper().getThread().getId()));
语句将记录 ThreadOne
的线程 ID,因为我们将线程的循环程序传递给处理程序。但是,被记录的 id 是主线程的。这是为什么?假设 handleMessage()
正在主线程上执行是否正确?
最佳答案
问题出在 ThreadOne
的 getLooper()
方法上。该方法返回 myLooper()与调用该方法的线程关联。
为了返回与 ThreadOne
关联的 Looper,我建议类的以下实现:
public class ThreadOne extends Thread {
private Looper mLooper = null;
@Override
public void run() {
Looper.prepare();
mLooper = Looper.myLooper();
Looper.loop();
}
Looper getLooper() {
return mLooper;
}
}
注意! ThreadOne
的 Looper 在运行之前为 null
。您不能更早地获得对它的非 null
引用。你必须 check if the thread is running在调用 getLooper()
方法之前。
附言您可能需要重新考虑获取 ThreadOne
的 Looper 的方法。拥有一个与 ThreadOne
关联的 Handler 可能就足够了 ( see the example of thread )。
关于java - 处理程序的 `handleMessage` 返回主线程的线程 ID 而不是工作线程的线程 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55781681/
我正在查看 NServiceBus v3.3.0.0,在我们的 MessageHandler 中它调用外部 WCF 服务。 如果 WCF 服务被同步调用并且该服务抛出异常,NServiceBus 会完
我有一个试图获取用户位置的线程。 当接收到位置时,“handler.sendMessage(msg)”被调用,并返回 true,但 handleMessage 永远不会被调用。 logcat 中没有错
我正在帮助我的 friend 开发一个 Android 应用程序,我不明白为什么我无法获取 Handle 函数,handleMessage() 被调用。我目前正在尝试通过蓝牙进行通信,我有一个处理写入
当我们在主 UI 线程中实例化一个 Handler 对象时(对于这个例子,假设我正在使用匿名内部类)。 Handler h = new Handler () { @override p
我有一个在单独进程中运行的服务。该服务在 onCreate() 方法中生成一个新线程。此线程将消息发送回服务。 如果我手动启动应用程序,一切正常 - 消息由我服务中的 Handler 接收。但在我的测
我正在编写一个程序,我在其中拍照,然后想将照片存储到 SD 卡中,最后尝试调整现有图像的大小(然后用旧图像覆盖)。 我的代码几乎在所有设备上都能正常工作,但只在三星设备上遇到这个问题。 当我捕捉图像时
我试图理解这个版本的 Message.obtain() http://developer.android.com/reference/android/os/Message.html#obtain%28
应该是这样的: Handler handler = new Handler() { @Override public void handleMessage(Message msg)
为什么 handleMessage 在我使用后台线程循环器创建处理程序时在 ui 线程中运行?此循环程序不属于 ui 线程,但 alertdialog 显示正确且没有崩溃,但应给出错误。 如果问题不清
我有一项服务可以从 Internet 接收命令并启动后台线程。该线程从服务传递一个处理程序(服务被绑定(bind)并传递给处理程序)并向处理程序发送消息以拍照。我坚持执行处理程序。 static Ha
我在 Eclipse 中使用向导将 JAX-WS 处理程序添加到我的项目中。它实现了 SOAPHandler。 然后,在 Project Explorer 中的 JAX-WS Web Services
此代码 fragment 在 CircleView 扩展 View 类中 private Handler handler = new Handler() { @Override pub
本文整理了Java中org.springframework.web.socket.WebSocketHandler.handleMessage()方法的一些代码示例,展示了WebSocketHandl
我有一个带有 Android.os.Handler 的 Activity ,用于与 openGL 线程通信。我想从 opengl 线程获取消息并根据消息数据在 Activity 中绘制一些 GUI,所
我正在尝试掌握 Android 中的多线程。我的目标是将数据从主线程发送到工作线程。 我在其 onCreate 中有一个包含以下代码的主要 Activity - protected void onCr
我正在尝试通过客户端访问 SOAP 请求和响应,以便计算每个调用的执行时间。我已经实现了 SOAPHandler要实现这一点,但 handleMessage方法没有被调用(断点没有被命中或没有日志被记
我的 Spring Boot 应用程序中有一个使用 Web 套接字的严重错误。随机发生的情况是,在我的 MyWebSocketHandler 类中,在 afterConnectionClosed 之后
我有一个扩展 android.os.Handler 的类。此处理程序的一个实例被传递给 Messenger 的构造函数。来自 getBinder 的 Messenger 的 IBinder 作为我服务
每隔几天,我都会收到一份应用程序崩溃报告,其中包含以下堆栈跟踪或其小变体(根据不同的 android 版本具有不同的行号) java.lang.NullPointerException at WebV
我使用 traceView,我看到 android.view.ViewRoot.handleMessage 76.4%这是什么意思?以及如何在我的源代码中找到这个错误? 最佳答案 这可能没问题。该值显
我是一名优秀的程序员,十分优秀!