将一个对象放入Handler消息中

发布于 2024-09-04 11:07:13 字数 663 浏览 4 评论 0原文

我需要从互联网下载图像, 在不同的线程中,
然后将处理程序消息中的图像对象发送到 UI 线程。

我已经有了这个:

...
Message msg = Message.obtain();

Bundle b = new Bundle();
b.putParcelable("MyObject", (Parcelable) object);
msg.setData(b);

handler.sendMessage(msg);

当我收到这条消息时, 我想提取该对象:

...
public void handleMessage(Message msg) {
    super.handleMessage(msg);

    MyObject objectRcvd = (MyObject) msg.getData().getParcelable("IpTile");
    addToCache(ipTile);
    mapView.invalidate();
}

但这给了我:

...java.lang.ClassCastException...

有人可以帮忙吗?

顺便问一下,这是最有效的方法吗
将对象传递给 UI 线程?

谢谢大家!

I need to download an image from the internet,
in a different thread,
and then send that image object in the handler message, to the UI thread.

I already have this:

...
Message msg = Message.obtain();

Bundle b = new Bundle();
b.putParcelable("MyObject", (Parcelable) object);
msg.setData(b);

handler.sendMessage(msg);

And when I receive this message,
I want to extract the object:

...
public void handleMessage(Message msg) {
    super.handleMessage(msg);

    MyObject objectRcvd = (MyObject) msg.getData().getParcelable("IpTile");
    addToCache(ipTile);
    mapView.invalidate();
}

But this is giving me:

...java.lang.ClassCastException...

Can anyone help?

And by the way, is this the most efficient way
to pass an object to the UI Thread?

Thank you all!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤云独去闲 2024-09-11 11:07:13

我知道我迟到了,但如果您在单个进程中使用该服务,则有一种更简单的方法。您可以使用此行将任意对象附加到您的消息

msg.obj = new CustomObject() // or whatever object you like

这对我当前的项目来说效果很好。
哦,我正在放弃使用 AsyncTask 对象,因为我相信它们过多地增加了代码耦合。

I know I'm late to the party, but there is an easier way if you are using the service within a single process. You can attach any arbitrary Object to your Message using this line:

msg.obj = new CustomObject() // or whatever object you like

This is working well for me in my current projects.
Oh, and I'm moving away from using AsyncTask objects, as I believe they increase code coupling too much.

美羊羊 2024-09-11 11:07:13

第一:你到底从哪里得到异常?将实例放入捆绑包时,还是检索实例时?

我相信你把事情搞混了。创建捆绑包时,您可以编写

b.putParcelable("MyObject", (Parcelable) object);

“因此您将实例“objet””分配给键“MyObject”。但是,在检索实例时,您可以这样写:

MyObject objectRcvd = (MyObject) msg.getData().getParcelable("IpTile");

这里,您正在从键“IpTile”检索实例。请注意“IpTile”!=“MyObject”。尝试使用以下内容来检索对象:

MyObject objectRcvd = (MyObject) msg.getData().getParcelable("MyObject");

或者相反,尝试用以下代码替换将实例放入捆绑包中的代码:

b.putParcelable("IpTile", (Parcelable) object);

另外要检查的几点:

  • MyObject 是否实现 可打包? (我想是的,否则你将无法编译)
  • 变量object是否包含实现Parcelable的实例?

First: Where exactly do you get the exception? When putting the instance into the bundle, or upon retrieving it?

I believe your mixing things up. When creating your bundle, you write

b.putParcelable("MyObject", (Parcelable) object);

So you are assigning the instance "objet " to the key "MyObject". But when retrieving your instance you write:

MyObject objectRcvd = (MyObject) msg.getData().getParcelable("IpTile");

Here, you are retrieving an instance from the key "IpTile". Note that "IpTile" != "MyObject". Try using the following to retrieve the object:

MyObject objectRcvd = (MyObject) msg.getData().getParcelable("MyObject");

or the other way around, try replacing your code which puts the instance into the bundle with this:

b.putParcelable("IpTile", (Parcelable) object);

Another few points to check:

  • Does the class MyObject implement Parcelable? (I suppose so, otherwise you wouldn't be able to compile)
  • Does the variable object contain an instance which implements Parcelable?
ぽ尐不点ル 2024-09-11 11:07:13

我会使用 AsyncTask 来执行此类操作。它允许您连接到您的 ui 线程以进行进度更新和完成下载等操作。下面的例子展示了应该如何做:

class GetImageTask extends AsyncTask<String, int[], Bitmap> {

  @Override
  protected Bitmap doInBackground(String... params) {
    Bitmap bitmap = null;

    // Anything done here is in a seperate thread to the UI thread 
    // Do you download from here

    // If you want to update the progress you can call
    publishProgress(int progress); // This passes to the onProgressUpdate method

    return bitmap; // This passes the bitmap to the onPostExecute method
  }

  @Override
  protected void onProgressUpdate(Integer... progress) {
    // This is on your UI thread, useful if you have a progressbar in your view
  }

  @Override
  protected void onPostExecute(Bitmap bitmapResult) {
    super.onPostExecute(bitmapResult);
    // This is back on your UI thread - Add your image to your view
    myImageView.setImageBitmap(bitmapResult);
  }
}

希望有帮助

I would use an AsyncTask for this kind of operation. It allows you to hook into your ui thread for things like progress updates and once you have finished your download. The example below shows how one should be done:

class GetImageTask extends AsyncTask<String, int[], Bitmap> {

  @Override
  protected Bitmap doInBackground(String... params) {
    Bitmap bitmap = null;

    // Anything done here is in a seperate thread to the UI thread 
    // Do you download from here

    // If you want to update the progress you can call
    publishProgress(int progress); // This passes to the onProgressUpdate method

    return bitmap; // This passes the bitmap to the onPostExecute method
  }

  @Override
  protected void onProgressUpdate(Integer... progress) {
    // This is on your UI thread, useful if you have a progressbar in your view
  }

  @Override
  protected void onPostExecute(Bitmap bitmapResult) {
    super.onPostExecute(bitmapResult);
    // This is back on your UI thread - Add your image to your view
    myImageView.setImageBitmap(bitmapResult);
  }
}

Hope that helps

把时间冻结 2024-09-11 11:07:13

如果您只转发一个对象,则可以使用 Message 类来完成,无需创建捆绑包 - 它更容易、更快。

Object myObject = new Object();
Message message = new Message();
message.obj = myObject;
message.what = 0;

mHandler.sendMessage(message); 

经典地检索它

@Override
public void handleMessage(Message msg) {
    super.handleMessage(msg);

    // ...
    Object object = (Object) msg.obj;  
}

享受:)

If you are forwarding only one object, you can do it using the Message class, you don't need to create a bundle - it's easier and faster.

Object myObject = new Object();
Message message = new Message();
message.obj = myObject;
message.what = 0;

mHandler.sendMessage(message); 

Retrieve it classicaly

@Override
public void handleMessage(Message msg) {
    super.handleMessage(msg);

    // ...
    Object object = (Object) msg.obj;  
}

Enjoy :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文