0

I want to create a method which can update(in particular time interval) the user about the progress of tasks it is performing & finally, can return a result true/false or any object.

For eg I have to create a method which encrypts or decrypts a file/directory. So I have to show the progress of encryption/decryption & at the end I have to return true or false value whether file is encrypted/decrypted successfully or not.

I want to create this in Android(Java) & the method should be an independent one.

Arun Badole
  • 10,977
  • 19
  • 67
  • 96

2 Answers2

0

You can use an AsyncTask

AsyncTask

Example

You can even put this in a service running alone even if the app is in background..

Community
  • 1
  • 1
allemattio
  • 1,836
  • 17
  • 33
0

you can use AsyncTask.

in AsyncTask you can do things in doInBackground. during the task you can post progress by calling publishProgress method which will be catched by onProgressUpdate method. also when the task is finished the onPostExecute method will be called. So that is what you want.

So all you need to do this something like that

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         // Do your task and during the task calculate the progress and call publishProgress to publish the progress
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         progress received
     }

     protected void onPostExecute(Long result) {
         task finihshed
     }
 }
stinepike
  • 54,068
  • 14
  • 92
  • 112