Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. public class MyActivity extends Activity {
  2.  
  3.     [ . . . ]
  4.     // Need handler for callbacks to the UI thread
  5.     final Handler mHandler = new Handler();
  6.  
  7.     // Create runnable for posting
  8.     final Runnable mUpdateResults = new Runnable() {
  9.         public void run() {
  10.             updateResultsInUi();
  11.         }
  12.     };
  13.  
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.  
  18.         [ . . . ]
  19.     }
  20.  
  21.     protected void startLongRunningOperation() {
  22.  
  23.         // Fire off a thread to do some work that we shouldn't do directly in the UI thread
  24.         Thread t = new Thread() {
  25.             public void run() {
  26.                 mResults = doSomethingExpensive();
  27.                 mHandler.post(mUpdateResults);
  28.             }
  29.         };
  30.         t.start();
  31.     }
  32.  
  33.     private void updateResultsInUi() {
  34.  
  35.         // Back in the UI thread -- update our UI elements based on the data in mResults
  36.         [ . . . ]
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement