Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put an object in Handler message

Tags:

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!

like image 859
Tsimmi Avatar asked Jun 09 '10 09:06

Tsimmi


People also ask

Which thread will process a message posted to a handler?

Android handles all the UI operations and input events from one single thread which is known as called the Main or UI thread. Android collects all events in this thread in a queue and processes this queue with an instance of the Looper class.

What does handler Post do?

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed at some point in the future; and (2) to enqueue an action to be performed on a different thread than your own. Scheduling messages is accomplished with the post(Runnable) , postAtTime(java.

What is Handler in android example?

A Handler is a threading class defined in the android. os package through which we can send and process Message and Runnable objects associated with a thread's MessageQueue . You start by creating a Handler instance. Then that instance gets associated with a single thread as well as that thread's message queue.


2 Answers

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.

like image 54
Caspar Harmer Avatar answered Apr 28 '23 01:04

Caspar Harmer


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?
like image 27
exhuma Avatar answered Apr 28 '23 01:04

exhuma