0

I would like to receive the message from my handler (from a service) and set the text to my editext in my UI. I am sending the message from background thread to main thread using handler inside the service. I created one handler in background thread and one in the main thread (in the constructor). Now I would like to send this message to my MainActivity and update UI. I have the following code.

MyService.java

public class myLocalThread extends Thread {

@SuppressLint("HandlerLeak")
myLocalThread() throws IOException {

mhandler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
    switch (msg.what) {
      case MSG:

        DeviceMessageStatus statusObject = (DeviceMessageStatus) msg.obj;
        statusClass = statusObject.getClass();
        try {
            statusField = statusClass.getField("screening_information");
            statusFieldValue = statusField.get(statusObject);
        Log.i("TAG", "Message OBJECT :" + statusFieldValue);
    }

  };
 }
}

I got the message forwarded from my background thread to mainthread but inside the service. How can I Update UI in the MainActivity. Should I create a another handler ? Here is what I did

MainActivity.java
Object statusFieldValue;
onCreate(Bundle savedInstanceState){
    number = (EditText) findViewById(R.id.dialpad);
    number.setText((Integer) statusFieldValue);
    Log.i("RL_RX", "onCreate: " + statusFieldValue);
}

This throws an nullPointerError the acitivity couldnot start. How can I retreive the message in the messagequeue of UI thread. What am I missing here ?

cantona_7
  • 1,127
  • 4
  • 21
  • 40
  • 1
    Possible duplicate of [Updating UI from a service (using a handler?)](https://stackoverflow.com/questions/7942083/updating-ui-from-a-service-using-a-handler) – Giddy Naya Aug 05 '19 at 10:11
  • could you show us whole code ??? your variable "statusFieldValue" is not in "MainActivity", i only see it in "MyService" – PangoSea Aug 05 '19 at 10:16
  • @PangoSea I just added it. This is what I have in MainActivity. How could I retreive the message from the Handler in UI thread. Thats where I am bit confused – cantona_7 Aug 05 '19 at 10:19
  • there are a lot of methods can go it. the simplest way is broadcastreceiver. as i can see, you wanna communicate between activity and services, in android doc, https://developer.android.com/guide/components/bound-services , bound-service is the best. – PangoSea Aug 05 '19 at 10:23
  • I have the used Broadcast receiver to communicate from `activity` to service already. – cantona_7 Aug 05 '19 at 10:25
  • "statusFieldValue" is a memeber of MainActivity, when you update the value ??? in MyService ??? how you hold the reference – PangoSea Aug 05 '19 at 10:30
  • I update the value from the service. you are right – cantona_7 Aug 05 '19 at 10:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/197487/discussion-between-cantona-7-and-pangosea). – cantona_7 Aug 05 '19 at 11:02

1 Answers1

1

you declare a handler naming "mhandler",but in mhandler, you still use mhandler to send messages ,is it a infinite loop ???

PangoSea
  • 591
  • 3
  • 11