Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. package com.example.async;
  2.  
  3. import android.app.Activity;
  4. import android.os.AsyncTask;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11.  
  12. /**
  13.  * @author Prabu AsyncTask exmple
  14.  *
  15.  */
  16. public class MainActivity extends Activity {
  17.     private Button button;
  18.     private EditText time;
  19.     private TextView finalResult;
  20.  
  21.     @Override
  22.     protected void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.activity_main);
  25.         time = (EditText) findViewById(R.id.et_time);
  26.         button = (Button) findViewById(R.id.btn_do_it);
  27.         finalResult = (TextView) findViewById(R.id.tv_result);
  28.         button.setOnClickListener(new View.OnClickListener() {
  29.             @Override
  30.             public void onClick(View v) {
  31.                 AsyncTaskRunner runner = new AsyncTaskRunner();
  32.                 String sleepTime = time.getText().toString();
  33.                 runner.execute(sleepTime);
  34.             }
  35.         });
  36.     }
  37.  
  38.     @Override
  39.     public boolean onCreateOptionsMenu(Menu menu) {
  40.         // Inflate the menu; this adds items to the action bar if it is present.
  41.         getMenuInflater().inflate(R.menu.activity_main, menu);
  42.         return true;
  43.     }
  44.  
  45.     private class AsyncTaskRunner extends AsyncTask<String, String, String> {
  46.  
  47.         private String resp;
  48.  
  49.         @Override
  50.         protected String doInBackground(String... params) {
  51.             publishProgress("Sleeping..."); // Calls onProgressUpdate()
  52.             try {
  53.                 // Do your long operations here and return the result
  54.                 int time = Integer.parseInt(params[0]);
  55.                 // Sleeping for given time period
  56.                 Thread.sleep(time);
  57.                 resp = "Slept for " + time + " milliseconds";
  58.             } catch (InterruptedException e) {
  59.                 e.printStackTrace();
  60.                 resp = e.getMessage();
  61.             } catch (Exception e) {
  62.                 e.printStackTrace();
  63.                 resp = e.getMessage();
  64.             }
  65.             return resp;
  66.         }
  67.  
  68.         /*
  69.          * (non-Javadoc)
  70.          *
  71.          * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
  72.          */
  73.         @Override
  74.         protected void onPostExecute(String result) {
  75.             // execution of result of Long time consuming operation
  76.             finalResult.setText(result);
  77.         }
  78.  
  79.         /*
  80.          * (non-Javadoc)
  81.          *
  82.          * @see android.os.AsyncTask#onPreExecute()
  83.          */
  84.         @Override
  85.         protected void onPreExecute() {
  86.             // Things to be done before execution of long running operation. For
  87.             // example showing ProgessDialog
  88.         }
  89.  
  90.         /*
  91.          * (non-Javadoc)
  92.          *
  93.          * @see android.os.AsyncTask#onProgressUpdate(Progress[])
  94.          */
  95.         @Override
  96.         protected void onProgressUpdate(String... text) {
  97.             finalResult.setText(text[0]);
  98.             // Things to be done while execution of long running operation is in
  99.             // progress. For example updating ProgessDialog
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement