http://developer.android.com/reference/android/os/Handler.html
package : android.os public class Handler extends Object
The handler allows you to send and process the Message and Runnable objects associated with the MessageQueue stream. Each Handler instance is associated with one thread and this thread's message queue. When you create a new handler, it is attached to the thread / message queue of the thread that creates it - from this point it will deliver messages and executable files to the message queue and execute them as they exit the queue message.
There are two main uses for a handler:
- for scheduling messages and executables to be executed as some point in the future; and
- to set an action to be performed on a thread other than your own.
Exmaple 1
use the handler on the application splash page.
if (!isFirstIn) { mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS); } else { mHandler.sendEmptyMessageDelayed(GO_GUIDE, SPLASH_DELAY_MILLIS); } private Handler mHandler = new Handler() { public void handleMessage(Message msg) { if(isAuto){ switch (msg.what) { case GO_HOME: goHome(); break; case GO_GUIDE: goGuide(); break; } } super.handleMessage(msg); } }; private void goHome() { Intent intent = new Intent(SplashActivity.this, MainAct.class); SplashActivity.this.startActivity(intent); SplashActivity.this.finish(); } private void goGuide() { Intent intent = new Intent(SplashActivity.this, GuideActivity.class); SplashActivity.this.startActivity(intent); SplashActivity.this.finish(); }
Example 2
use the handler request service in the child thread if the request may take some time.
new Thread(new Runnable(){ @Override public void run() { String versionPath = Parameters.getCheckVersionPath(); String result = RequestHelper.doGet(versionPath, null); Message msg = new Message(); Bundle data = new Bundle(); data.putString("result",result); msg.setData(data); handler1.sendMessage(msg); } }).start(); handler1 = new Handler(){ @Override public void handleMessage(Message msg) { String result = msg.getData().getString("result"); JSONObject obj; try { obj = new JSONObject(result); Map<String, String> versionInfo = Helper.getSoftwareVersion(obj); if (versionInfo != null) { newVersion = versionInfo.get("version"); updateUrl = versionInfo.get("url"); } } catch (JSONException e) { Log.w("net work error!", e); } } };
Example 3
use Handler and Timer to update progress bar.
logobar = (ImageView) findViewById(R.id.splash_bar);//progress bar. logobarClipe = (ClipDrawable) logobar.getBackground(); timer = new Timer(); timer.schedule(new TimerTask() { public void run() { updateLogoBarHandler.sendEmptyMessage(0); }}, 0, rate); /************************************************************************************** *2. Handler */ //update progress bar. private Handler updateLogoBarHandler = new Handler() { public void handleMessage(Message msg) { if(logobarClipe.getLevel() < 10000){ //1.update image. logobarClipe.setLevel(logobarClipe.getLevel() + rate*2); //2.update text. float percent = logobarClipe.getLevel() /100; String percentTxtVerbose = String.valueOf(percent); String percentTxt = percentTxtVerbose.substring(0, percentTxtVerbose.indexOf('.')) + "%"; bartxt.setText(percentTxt); }else{ timer.cancel(); } super.handleMessage(msg); } };
Eddy
source share