LittleFunny
LittleFunny

Reputation: 8375

Handler on android not receiving the message

I have a code running inside the Thread. I tried to use the handler to do receive the message from the thread so i can update the UI. Unfortunately, the message didn't get send to the handler.

This is my code snippet inside the run method of the Thread

                    ChromaticLayout chromatic =  new ChromaticLayout(mPartition, mDeviceWidth, mDeviceHeight, mData);
                    chromatic.execute(new ChromaticLayout.LayoutCallback() {

                        @Override
                        public synchronized void retrieveResult(Object[][] data) {
                            // TODO Auto-generated method stub
                            mPhotoData.clear();
                            Log.w("CALLBACK", "start");

                            for (int i=0; i<data.length; i++) 
                            {
                                PhotoFrameData[] row = new PhotoFrameData[data[i].length];

                                for (int j=0; j<data[i].length; j++) {
                                    if (j==0)
                                    Log.w("CALLBACK", "Width = " + ((PhotoFrameData) data[i][j]).getRectangle().width() + " height = " +  ((PhotoFrameData) data[i][j]).getRectangle().height() );

                                    row[j] = (PhotoFrameData) data[i][j];
                                }
                                mPhotoData.add(row);

                            }
                            Log.w("CALLBACK", "end");

                            PhotoFrameAdapter.this.handle.post(new Runnable(){

                                @Override
                                public void run() {
                                    // TODO Auto-generated method stub
                                    PhotoFrameAdapter.this.handle.sendEmptyMessage(1);
                                } });
                            //if (!PhotoFrameAdapter.this.handle.sendEmptyMessage(1))
                            //  Log.w("CALLBACK", "Handle not working");
                        }});

                }

The is the receiving message of the handler:

protected Handler handle = new Handler() {

        public void handleMessage(Bundle message) {
            //PhotoFrameAdapter.this.notifyDataSetChanged();
            mListener.dataLoaded(this);
        }
    };

What make it not adding to the message queue and call the handleMessage? Thanks

Upvotes: 0

Views: 1312

Answers (2)

Philio
Philio

Reputation: 4185

Another way you can use a Handler is as follows (it's perhaps a simpler implementation in many cases):

Define the hander on the UI thread:

private Handler mHandler = new Handler();

Then from your background thread just post a Runnable with the code you want to run:

mHandler.post(new Runnable() {
    @Override
    public void run() {
        // Code to run here
    }
});

Upvotes: 1

Fatih Santalu
Fatih Santalu

Reputation: 4701

try this:

private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 1:
                //do staff
                break;
            }
        };
    };

Upvotes: 2

Related Questions