Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. // An AsyncTask definition
  2. // The view in your UI that you want to update from the AsyncTask
  3.  
  4. private ProgressBar asyncProgress;
  5. private TextView asyncTextView;
  6.  
  7. private class MyAsyncTask extends AsyncTask<String, Integer, String> {
  8. @Override
  9. protected String doInBackground(String... parameter) {
  10. // Move to a background thread
  11. String result = "";
  12. int myProgress = 0;
  13.  
  14. int inputLength = parameter[0].length();
  15.  
  16. // perform background processing task, update myProgress
  17. for(int i = 1; i <= inputLength; i++) {
  18. myProgress = i;
  19. result = result + parameter[0].charAt(inputLength - i);
  20. try {
  21. Thread.sleep(100);
  22. } catch (InterruptedException e) { }
  23. publishProgress(myProgress);
  24. }
  25. // return the value to be passed to onPostExecute
  26. return result;
  27. }
  28.  
  29. @Override
  30. protected void onPreExecute() {
  31. asyncProgress.setVisibility(View.VISIBLE);
  32. }
  33.  
  34. @Override
  35. protected void onProgressUpdate(Integer... progress) {
  36. asyncProgress.setProgress(progress[0]);
  37. }
  38.  
  39. @Override
  40. protected void onPostExecute(String result) {
  41. asyncProgress.setVisibility(View.GONE);
  42. asyncTextView.setText(result);
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement