Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyActivity extends Activity {
- [ . . . ]
- // Need handler for callbacks to the UI thread
- final Handler mHandler = new Handler();
- // Create runnable for posting
- final Runnable mUpdateResults = new Runnable() {
- public void run() {
- updateResultsInUi();
- }
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- [ . . . ]
- }
- protected void startLongRunningOperation() {
- // Fire off a thread to do some work that we shouldn't do directly in the UI thread
- Thread t = new Thread() {
- public void run() {
- mResults = doSomethingExpensive();
- mHandler.post(mUpdateResults);
- }
- };
- t.start();
- }
- private void updateResultsInUi() {
- // Back in the UI thread -- update our UI elements based on the data in mResults
- [ . . . ]
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement