-1

I am trying the following scenario

1)send a message from UI thread , to worker using HandlerThread

2)Read it using handlemessage

3)send it back to a text field in UI

I am using the below code , The issue now is that , the message in the handlemessage is coming as null

  public class MainActivity extends AppCompatActivity    {

    private TextView serverStatus;
    private TextView clientStatus;

    private Handler mUiHandler = new Handler();



    private BluetoothServerSocket  serverSocket;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        final Button readButton = (Button) findViewById(R.id.button2);

        readButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //TODO Code to manage read data from client here !!
               /* mWorkerThread2 = new MyWorkerThread("myWorkerThread2");
                mWorkerThread2.start();
*/

            }});
        final Button submitButton = (Button) findViewById(R.id.button);
        serverStatus=(TextView) findViewById(R.id.editText);

        submitButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                HandlerThread myThread = new HandlerThread("Worker Thread");
                myThread.start();
                Looper mLooper = myThread.getLooper();
                MyHandler mHandler = new MyHandler(mLooper);
              /*  Bundle data = new Bundle();
                data.put
                msg.setData(data);*/
                Message msg = mHandler.obtainMessage();

                msg.obj =  serverStatus.getText().toString();// Some Arbitrary object
              /*  Toast.makeText(getApplicationContext(), msg.obj.toString(), Toast.LENGTH_SHORT).show();*/
                        mHandler.sendMessage(msg);
            }});
    }

    class MyHandler extends Handler {
        public MyHandler(Looper myLooper) {
            super(myLooper);
        }
        public void handleMessage(final Message msg) {
            //final String text=msg.getData().getString("");
         mUiHandler.post(new Runnable() {
             @Override
             public void run() {
                 String ms=String.valueOf(msg.obj);
                 serverStatus.setText("from server !!!  "+ms );
             }
         });
        }
    }

}
dileepVikram
  • 890
  • 4
  • 14
  • 30

1 Answers1

0

As per comments from @pskink , I have used CallBack . Below code works fine

public class MainActivity extends AppCompatActivity    {

    private TextView serverStatus;
    private TextView clientStatus;

    private Handler mUiHandler = new Handler();
    Handler mHtHandler;
   // Handler mUiHandler;



    private BluetoothServerSocket  serverSocket;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        final Button readButton = (Button) findViewById(R.id.button2);

        readButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //TODO Code to manage read data from client here !!


            }});
        final Button submitButton = (Button) findViewById(R.id.button);
        serverStatus=(TextView) findViewById(R.id.editText);

        submitButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                HandlerThread ht = new HandlerThread("MySuperAwsomeHandlerThread");
                ht.start();
                Handler.Callback callback = new Handler.Callback() {
                    @Override
                    public boolean handleMessage(Message msg) {
                        if (msg.what == 0) {

                           final Message msg1=mUiHandler.obtainMessage(0);
                            msg1.obj=msg.obj;

                            mUiHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    final String ms=(String)msg1.obj;
                                    serverStatus.setText("from server !!!  "+ms );
                                }
                            });
                        }

                        return false;
                    }
                };
                mHtHandler = new Handler(ht.getLooper(), callback);
                Message msg=mHtHandler.obtainMessage(0);
                msg.obj =  serverStatus.getText().toString();// Some Arbitrary object
                mHtHandler.sendMessage(msg);


            }});
    }

}
dileepVikram
  • 890
  • 4
  • 14
  • 30
  • ok so instead of `final Message msg1= ... ` try `final String ms=(String)msg.obj;` and use it in `serverStatus.setText()` method - but actually why to mix `"Message"` and `"Runnable"` styles when using `Hnadler`s? – pskink Sep 14 '18 at 10:36
  • @pskink Will reply soon , I am new to android . My current understanding is that I cannot set values to UI from a different thread.So I am using the handler of UI thread and posting task as Runnable . Is that correct ?? – dileepVikram Sep 14 '18 at 18:24
  • 1
    you can send Message as well, so either post Runnables between two Handlers or send Messages, don't mix both - it will work but it looks bad – pskink Sep 14 '18 at 18:28
  • okay , Thank you , But how can I access the UI elements , while using messages – dileepVikram Sep 14 '18 at 18:29
  • in Handler#handleMessage method – pskink Sep 14 '18 at 18:39
  • You mean , I need to write one CallBack for the UI handler as well , right ? – dileepVikram Sep 14 '18 at 18:41
  • 1
    You can 1) extend Handler and override handleMessage or 2) implement Callback interface as I did in my example, with the second approach you can use one callback for both handlers – pskink Sep 14 '18 at 18:48
  • Okay , will try it – dileepVikram Sep 14 '18 at 18:49
  • Please don't delete your comments so that I can refer later – dileepVikram Sep 14 '18 at 19:02