简体   繁体   中英

how to use handler in android to work in main thread?

I am able to update UI using runOnUiThread(new Runnable() { .

I think there is another way to update UI using Handler. Could you please tell me how I will update UI using handler.

I used like that

@Override
public void getWebserviceResponse(final String result) {
    // TODO Auto-generated method stub
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            refreshUi(result);
        }
    });


}

same thing how I will achieve with handler ?

Update

private Handler messageHandler = new Handler() {
        public void handleMessage(Message msg) {
            final int what = msg.what;
            switch(what) {
            case DO_UPDATE_TEXT: doUpdate(); break;

            }

        }
    };



@Override
    public void getWebserviceResponse(final String result) {

        messageHandler.sendEmptyMessage(DO_UPDATE_TEXT);


    }

I need to send String result with DO_UPDATE_TEXT

            private final Handler handler = new Handler() {

                    public void handleMessage(String result) {


                        if ((null != response)) {

                            // ALERT MESSAGE
                             refreshUi(result);
                        }
                        else
                        {

                                // ALERT MESSAGE
                                Toast.makeText(
                                        getBaseContext(),
                                        "Not Result to show",
                                        Toast.LENGTH_SHORT).show();
                        }    

                    }
                };
    final Handler h = new Handler();
    h.post(new Runnable() {
        @Override
        public void run() {

             long millis =(long)currentTime(); // Get current time in Milli Seconds                 
             dateAndTime.setText(getDate(millis, "dd/MM/yyyy hh:mm:ss.SSS")); // Set it as Date&Time                    
             h.postDelayed(this, 1000);
        }
    });

try like this may help you,

private Handler messageHandler = new Handler() {

                public void handleMessage(Message msg) {
                    final int what = msg.what;
                    switch(what) {
                    case DO_UPDATE_TEXT: 
                     String result = msg.arg1;
                     doUpdate(); 
                     break;
                    }
                }
            };

    @Override
    public void getWebserviceResponse(final String result) {
           Message msg = handler.obtainMessage();
           msg.what = DO_UPDATE_TEXT;
           msg.obj = result;

           messageHandler.sendMessage(msg);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM