0

I have a doubt in the usage of handlers in the following scenario

I have a an activity class as follows:

public class MyActivity extends Activity {

    ...
    ...

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)  {
       ...
    }
}

and a BluetoothClientConnection class as follows

public class BTClientConnection extends Thread {
    public void run {
        ...
    }
}

Now I want to update the UI using handlers. How should I be doing it? Should I create a public handler variable and refer to it directly from my BluetoothClientConnection code? Is this the best practice as I would be directly coupling with the MyActivity class.

From Gennadii Saprykin answer should the final code be

public class MyActivity extends Activity {

    ...
    private ActivityHandler activityhandler = new ActivityHandler();

    static class ActivityHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {            

        } 
        ...
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)  {
       ...
       BTClientConnection btClientConnection = new BTClientConnection(..);
       btClientConnection.start();
    }
}

public class BTClientConnection extends Thread {
    private static final Handler UI_HANDLER = new Handler(Looper.getMainLooper());

    public void run {
        Message message = new Message();
        UI_HANDLER.sendMessage(message);
    }
}

1 Answers1

0

Create a UI Handler inside your background Thread this way:

private static final Handler UI_HANDLER = new Handler(Looper.getMainLooper());

then you can send a message or post a Runnable to UI thread. To handle message inside your activity you can create another handler inside your activity and override handleMessage method like this:

    @Override
    public void handleMessage(Message inputMessage) {
        if (message.what == YOUR_MESSAGE_CODE) {
           ...
        }
    }
Gennadii Saprykin
  • 4,505
  • 8
  • 31
  • 41
  • I have a buffer from network. Can I send this as a Message? If I get incoming data and I change the buffer wouldn't the data accessed in the UI thread get corrupted? Should I synchronize the access in some way or just sending a message would be enough? – mindentropy Jul 15 '15 at 18:34
  • If a buffer is not big (less than 500kb) you can send it as a Message, otherwise you will need to store it somewhere, e.g. to file, and then read from that file when message is handled. Handler uses a Looper which uses a MessageQueue. It is always sequential, so, it is already synchronized for you. You can't handle messages on the same Looper concurrently. – Gennadii Saprykin Jul 15 '15 at 18:39
  • Sorry, the limit is 1MB, see this link: http://stackoverflow.com/questions/8552514/is-there-some-limits-in-android-bundle – Gennadii Saprykin Jul 15 '15 at 18:49
  • The handleMessage should be new Handler() { public void handleMessage(Message inputMessage) } in the activity code? – mindentropy Jul 15 '15 at 19:27
  • I am sorry. What I meant was in the MyActivity class. – mindentropy Jul 15 '15 at 19:38
  • Yes, create a new Handler in MyActivity and override handleMessage method there. Also, don't use anonymous class here because you will have a memory leak in your activity. So, the handler should be static: static class MyActivityHandler extends Handler { public void handleMessage(Message msg) {} } – Gennadii Saprykin Jul 15 '15 at 19:41
  • I further edited the code in the main question. Is it fine? I am still not able to receive any messages. – mindentropy Jul 15 '15 at 20:00
  • it is fine. How do you instantiate ActivityHandler? – Gennadii Saprykin Jul 15 '15 at 21:28
  • Your code looks fine, when you send a message from UI_HANDLER the one in activity should receive it. Are you sure you send a message but handleMessage is not called? – Gennadii Saprykin Jul 16 '15 at 17:11
  • I don't get any message. Could you post a simple sample code so I can verify. – mindentropy Jul 17 '15 at 06:59
  • I'm sorry I'm not sure what to post, your code should work.. please make sure all these steps perform in the given order: 1. Instance of `ActivityHandler` is created, 2. `onSharedPreferenceChanged` executed, 3. `run` executed, 4. `handleMessage` in activity handler executed. From what you said everything is right except #4 is not executed. That doesn't seem right, probably some other code affects this, I'm not sure. You can read more about handlers here: https://developer.android.com/training/multiple-threads/communicate-ui.html – Gennadii Saprykin Jul 17 '15 at 15:57
  • Copy paste the above code and run. It should not work for you too. – mindentropy Aug 09 '15 at 17:41