Skip to content

Instantly share code, notes, and snippets.

@ErikHellman
Created March 23, 2016 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ErikHellman/148434264edb186d5498 to your computer and use it in GitHub Desktop.
Save ErikHellman/148434264edb186d5498 to your computer and use it in GitHub Desktop.
Demo of using Handlers for async operations in Android
public class MainActivity extends AppCompatActivity {
private static final int MSG_LONG_RUNNING_OPERATION = 101;
private static final int MSG_UPDATE_UI = 102;
private static final long THRITY_SECONDS = 30000;
private Handler bgHandler;
private Handler uiHandler;
private Handler.Callback callback;
private boolean doAgain = false;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Start our thread for background operations
HandlerThread handlerThread = new HandlerThread("bg-thread");
handlerThread.start();
callback = new Handler.Callback() {
@Override public boolean handleMessage(Message msg) {
switch (msg.what) {
case MSG_LONG_RUNNING_OPERATION:
doLongRunningOperation();
break;
case MSG_UPDATE_UI:
doUpdateUi((Bitmap) msg.obj);
break;
}
return true;
}
};
bgHandler = new Handler(handlerThread.getLooper(), callback);
uiHandler = new Handler(Looper.getMainLooper(), callback);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
bgHandler.sendEmptyMessage(MSG_LONG_RUNNING_OPERATION);
}
});
}
@Override protected void onDestroy() {
super.onDestroy();
doAgain = false;
bgHandler.removeCallbacksAndMessages(null);
uiHandler.removeCallbacksAndMessages(null);
// Shut down the background thread
bgHandler.getLooper().quit();
}
@WorkerThread private void doLongRunningOperation() {
SystemClock.sleep(10000);
Bitmap result = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
uiHandler.obtainMessage(MSG_UPDATE_UI, result).sendToTarget();
if (doAgain) {
bgHandler.sendEmptyMessageDelayed(MSG_LONG_RUNNING_OPERATION, THRITY_SECONDS);
}
}
@MainThread private void doUpdateUi(Bitmap bitmap) {
if (!doAgain) {
// Do UI work
}
}
@Override public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment