Skip to content

Instantly share code, notes, and snippets.

@unixnme
Last active February 12, 2017 04:09
Unresponsive App Example
package com.blogspot.unixnme.handlerexample;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class HandlerExample extends Activity {
private static String TAG = HandlerExample.class.getSimpleName();
private Button taskButton;
private Button incrementButton;
private TextView textView;
private int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_handler_example);
textView = (TextView) findViewById(R.id.text_vew);
taskButton = (Button) findViewById(R.id.button);
incrementButton = (Button) findViewById(R.id.increment_button);
// when the taskButton is pressed, do some long task, simulated by sleep
taskButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
taskButton.setText("Busy");
Thread task = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(3000);
} catch (Throwable t) {
Log.d(TAG, t.getMessage());
}
}
});
task.start();
try {
task.join();
} catch (Throwable t) {
Log.d(TAG, t.getMessage());
}
taskButton.setText("Ready");
}
});
// when the incrementButton is pressed, it will simply increment the counter
incrementButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(Integer.toString(++counter));
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment