Question

I dont really understand msg.what? If someone can explain how to use it so my switch statement of my handler can separate out the changes in textviews I would really appeciate it thank you so much in advanced.

This is my Handler:

    mHandler = new Handler() {
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch (msg.what){

            }
            Bundle bundle = msg.getData();
            String string = bundle.getString("outKey");
            display.setText(string);
            }

        };

This is where the msg is coming from:

public class myThread extends Thread {
    String outString;
    Thread hello = new Thread() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            super.run();
    String P1 = "Enter Point 1.";

            Message msg1 = Message.obtain();
            Bundle bundle1 = new Bundle();

            bundle1.putString("P1Key", P1);
            msg1.setData(bundle1);
            msg1.what = POINTONE;
            mHandler.sendMessage(msg1);
            synchronized(Lock){
                try {
                    Lock.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
         outString = call.getCoords();

            }
            Message msg = Message.obtain();
            Bundle bundle = new Bundle();

            bundle.putString("outKey", outString);
            msg.setData(bundle);
            mHandler.sendMessage(msg);

        }

    };
Was it helpful?

Solution

msg.what is used to relay a simple int without having to worry about adding it to the message. Your switch statement can treat msg.what as an integer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top