Created
July 27, 2014 13:25
-
-
Save ErikHellman/df18e23066285d0cfbbe to your computer and use it in GitHub Desktop.
Android sample showing how to use Handlers for efficient foreground/background operations.
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 se.hellsoft.demo.codesnippetdemo; | |
import android.app.Activity; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.os.HandlerThread; | |
import android.os.Message; | |
import android.os.SystemClock; | |
import android.view.View; | |
import android.widget.EditText; | |
import android.widget.ProgressBar; | |
/** | |
* Example to illustrate how to work with the Handler class in Android. | |
* This example is very naive and is nothing you should do in real life (networking should be handled by a service). | |
* However, it serves to show how you can use two Handlers to perform operations on background and foreground thread | |
* without having to allocate new objects all the time. | |
*/ | |
public class MyActivity extends Activity { | |
private static final int START_DOWNLOAD_MSG = 1001; | |
private static final int UPDATE_PROGRESS_MSG = 2002; | |
private static final int TOGGLE_START_BTN_MSG = 3003; | |
private Handler mBackgroundHandler; | |
private Handler mForegroundHandler; | |
private boolean mPaused = false; | |
class MyCallback implements Handler.Callback { | |
@Override | |
public boolean handleMessage(Message msg) { | |
switch (msg.what) { | |
case START_DOWNLOAD_MSG: | |
startDownload((Uri) msg.obj); | |
break; | |
case UPDATE_PROGRESS_MSG: | |
if (!mPaused) { | |
ProgressBar progressBar = ((ProgressBar) findViewById(R.id.download_progress)); | |
if (progressBar != null) { | |
progressBar.setProgress(msg.arg1); | |
progressBar.invalidate(); | |
} | |
} | |
break; | |
case TOGGLE_START_BTN_MSG: | |
if (!mPaused) { | |
View view = findViewById(R.id.start_download_btn); | |
if(view != null) { | |
view.setEnabled((Boolean) msg.obj); | |
} | |
} | |
break; | |
} | |
return true; | |
} | |
} | |
private void startDownload(Uri url) { | |
// Disable the button while downloading... | |
mForegroundHandler.obtainMessage(TOGGLE_START_BTN_MSG, false).sendToTarget(); | |
// Fake downloading of file... | |
for(int i = 0; i < 1000; i++) { | |
SystemClock.sleep(50); | |
mForegroundHandler.obtainMessage(UPDATE_PROGRESS_MSG, i, 0).sendToTarget(); | |
} | |
// Enable button again... | |
mForegroundHandler.obtainMessage(TOGGLE_START_BTN_MSG, true).sendToTarget(); | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
HandlerThread handlerThread = new HandlerThread("BackgroundThread"); | |
handlerThread.start(); | |
MyCallback callback = new MyCallback(); | |
mBackgroundHandler = new Handler(handlerThread.getLooper(), callback); | |
mForegroundHandler = new Handler(callback); | |
setContentView(R.layout.activity_my); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
mPaused = false; | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
mPaused = true; | |
} | |
@Override | |
protected void onDestroy() { | |
mBackgroundHandler.removeMessages(START_DOWNLOAD_MSG); | |
mBackgroundHandler.getLooper().quit(); | |
super.onDestroy(); | |
} | |
/** | |
* Callback for button click | |
* | |
* @param view | |
*/ | |
public void doStartDownload(View view) { | |
String fileUri = ((EditText) findViewById(R.id.file_url)).getText().toString(); | |
Uri uri = Uri.parse(fileUri); | |
mBackgroundHandler.obtainMessage(START_DOWNLOAD_MSG, uri).sendToTarget(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment