Created
March 3, 2014 21:34
-
-
Save csdear/eae2d7a1c9ede5c62075 to your computer and use it in GitHub Desktop.
Handler : Messages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package course.examples.Threading.ThreadingHandlerMessages; | |
| import android.app.Activity; | |
| import android.graphics.Bitmap; | |
| import android.graphics.BitmapFactory; | |
| import android.os.Bundle; | |
| import android.os.Handler; | |
| import android.os.Message; | |
| import android.view.View; | |
| import android.view.View.OnClickListener; | |
| import android.widget.Button; | |
| import android.widget.ImageView; | |
| import android.widget.ProgressBar; | |
| import android.widget.Toast; | |
| public class HandlerMessagesActivity extends Activity { | |
| private final static int SET_PROGRESS_BAR_VISIBILITY = 0; | |
| private final static int PROGRESS_UPDATE = 1; | |
| private final static int SET_BITMAP = 2; | |
| private ImageView mImageView; | |
| private ProgressBar mProgressBar; | |
| private int mDelay = 500; | |
| //1. Create a new handler, to handle work in the main UI thread. | |
| Handler handler = new Handler() { | |
| @Override | |
| //2. handleMessage() that starts by checking contents of the message. | |
| // Creates a switch case to take the right action for that message code. | |
| // Each one of these 3 cases are being "listened" for and respective code executed. | |
| public void handleMessage(Message msg) { | |
| switch (msg.what) { | |
| case SET_PROGRESS_BAR_VISIBILITY: { | |
| mProgressBar.setVisibility((Integer) msg.obj); | |
| break; | |
| } | |
| case PROGRESS_UPDATE: { | |
| mProgressBar.setProgress((Integer) msg.obj); | |
| break; | |
| } | |
| case SET_BITMAP: { | |
| mImageView.setImageBitmap((Bitmap) msg.obj); | |
| break; | |
| } | |
| } | |
| } | |
| }; | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.main); | |
| mImageView = (ImageView) findViewById(R.id.imageView); | |
| mProgressBar = (ProgressBar) findViewById(R.id.progressBar); | |
| //3. load button icon onClick creates a new thread for the LoadIconTask. | |
| final Button button = (Button) findViewById(R.id.loadButton); | |
| button.setOnClickListener(new OnClickListener() { | |
| public void onClick(View v) { | |
| new Thread(new LoadIconTask(R.drawable.painter, handler)) | |
| .start(); | |
| } | |
| }); | |
| final Button otherButton = (Button) findViewById(R.id.otherButton); | |
| otherButton.setOnClickListener(new OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| Toast.makeText(HandlerMessagesActivity.this, "I'm Working", | |
| Toast.LENGTH_SHORT).show(); | |
| } | |
| }); | |
| } | |
| private class LoadIconTask implements Runnable { | |
| private final int resId; | |
| private final Handler handler; | |
| LoadIconTask(int resId, Handler handler) { | |
| this.resId = resId; | |
| this.handler = handler; | |
| } | |
| //Run method obtains a message from the code set picked above. Sets visibility. | |
| //Then sends it to the handler and sends the message. | |
| public void run() { | |
| Message msg = handler.obtainMessage(SET_PROGRESS_BAR_VISIBILITY, | |
| ProgressBar.VISIBLE); | |
| handler.sendMessage(msg); | |
| //Loads the bitmap and then sends a message with the code set message PROGRESS_UPDATE. | |
| final Bitmap tmp = BitmapFactory.decodeResource(getResources(), | |
| resId); | |
| for (int i = 1; i < 11; i++) { | |
| sleep(); | |
| msg = handler.obtainMessage(PROGRESS_UPDATE, i * 10); | |
| handler.sendMessage(msg); | |
| } | |
| //Obtain and send a message to set the newly loaded bitmap on display. | |
| msg = handler.obtainMessage(SET_BITMAP, tmp); | |
| handler.sendMessage(msg); | |
| //Final, make progress bar invisible. | |
| msg = handler.obtainMessage(SET_PROGRESS_BAR_VISIBILITY, | |
| ProgressBar.INVISIBLE); | |
| handler.sendMessage(msg); | |
| } | |
| private void sleep() { | |
| try { | |
| Thread.sleep(mDelay); | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment