HanderThread class inherits from the Thread class, which encapsulates the Looper object, so that we do not care The Looper open and release details.
The HandlerThread object can be getLooper method to obtain a Looper object reference.
Methods in HandlerThread:
. getLooper(): This method returns the Looper associated with this thread.
. getThreadId(): Returns the identifier of this thread
. quit(): Ask the currently running looper to quit.
. run(): Calls the run() method of the Runnable object the receiver holds. If no Runnable is set, does nothing.
ProgressBar Example Using HandlerThread :
main.xml:
< << <
HanderThreadActivity.java :
package com.imrankhanandroid.HanderThread; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; import android.os.Looper; import android.os.Message; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; public class HanderThreadActivity extends Activity { /** Called when the activity is first created. */ ProgressBar bar; Button start; Runnable progressThread; MyHandler myHandler; TextView txtprogress; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.v("HanderThreadActivity :", "Activity Thread :"+Thread.currentThread().getId()); bar=(ProgressBar)findViewById(R.id.bar); txtprogress=(TextView)findViewById(R.id.txtprogress); start=(Button)findViewById(R.id.startButton); //HandlerThread asynchronous thread, not the main thread public HandlerThread handlerThread=new HandlerThread("handler_thread"); //GetLooper() method before you adjust the start() method handlerThread.start(); myHandler=new MyHandler(handlerThread.getLooper()); start.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub bar.setVisibility(ProgressBar.VISIBLE); myHandler.post(progressThread); } }); //Runnable inner class progressThread=new Runnable() { int percent=0; @Override public void run() { // TODO Auto-generated method stub percent +=5; Log.v("HanderThreadActivity :", "Runnable Thread :"+percent+"----"+Thread.currentThread().getId()); Message msg=new Message(); //Message object to pass parameters Bundle bundle=new Bundle(); bundle.putInt("percent", percent); msg.setData(bundle); myHandler.sendMessage(msg); try{ Thread.sleep(1000); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }; } class MyHandler extends Handler{ public MyHandler(Looper looper){ super(looper); } @Override public void handleMessage(Message msg){ Bundle bundle= msg.getData(); bar.setProgress(bundle.getInt("percent")); //MyHandler Thread : 8 Log.v("HanderThreadActivity :", "MyHandler Thread :"+bundle.getInt("percent")+"---"+Thread.currentThread().getId()); myHandler.post(progressThread); if(bundle.getInt("percent")>=90) { myHandler.removeCallbacks(progressThread); } } } }Download Source Code: HanderThreadExample
5 comments:
Good blog imran....
Good post thanks for sharing with us.
buy a logo for my business
this is really a helpful blog tahnks for sharing withb us,
are you guy's have an interested in web designing or logo designing then visit us?
Logo Designers
This is really awesome, thanks for the share. Also, visit our website if you are looking to buy amazing products in Pakistan:
Cheezain
Post a Comment