Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Using handlers in android.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 218
    Comment on it

    Brief Introduction

    In Android apps there are scenario  where we need to communicate the thread with our UI .Like downloading image from server and communicating to UI thread that downloading is completed or it is interrupted.

    Handlers are used as a mechanism or you can say IPC (inter process thread communication.)

    This demo shows you how to download image from server and set it on Imageview ,Let have a look on code.

     

    
    
    // load image on button click
    btnLaod.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // progressBar.setVisibility(View.VISIBLE);
            Thread thread = new Thread(new DownLaodImage());
            thread.start();
        }
    });
    
    // Handler to handle message based on thread communication
    private class UiHandler extends Handler{
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 0: {
    
                        progressBar.setVisibility(View.VISIBLE);
                        break;
                    }
                    case 1: {
                        progressBar.setVisibility(View.GONE);
                        imageView.setImageBitmap((Bitmap) msg.obj);
                        break;
                    }
                }
            }
        }
    
    
        private class DownLaodImage implements Runnable{
    
            @Override
            public void run() {
                handler.obtainMessage(0).sendToTarget();
                InputStream is = null;
                try {
                    // Download image over the network
                    URL url = new URL("http://www.gstatic.com/webp/gallery/2.jpg");
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("GET");
                    httpURLConnection.setDoInput(true);
                    httpURLConnection.connect();
                    is = httpURLConnection.getInputStream();
    
                    // Decode the byte payload into a bitmap
                    final Bitmap bitmapImage = BitmapFactory.decodeStream(is);
                    // you are sending status 1 that is image is downloaded completely and sending  object as bitmap to target 
                    handler.obtainMessage(1, bitmapImage).sendToTarget();
                } catch (IOException ignore) {
                } finally {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException ignore) {
                        }
                    }
                }
            }
        }
    
    

    Image here is downloaded in  background thread and obtained message is send to target.

    public final Message obtainMessage(int what, Object obj)

    int what , here denotes the status of the worker thread.

    Object ,here denotes what object is being passed (Bitmap in this example).

    
    
    // these are some different methods we can use with handlers.
    public final boolean sendMessageDelayed(Message msg, long delayMillis)
    public final boolean sendMessageAtFrontOfQueue(Message msg)
    public boolean sendMessageAtTime(Message msg, long uptimeMillis)

     

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: