Checking AsyncTask Status with Another Action

I am using AsyncTask to convert an image to a base64 value. The task runs in the background and the application proceeds to the next step. How can I check the status of AsyncTask to check if it is complete or not ...

My asynthetic code ...

 public class Asyncimg extends AsyncTask<Void, Integer, String> { //for converting images to base64 protected void onPreExecute (){ //disbaling sync button on converting pic to base64 } protected String doInBackground(Void...arg0) { Cursor cursor = mydb.getDat1(); //fetching the image location cursor.moveToFirst(); while (!cursor.isAfterLast()) { for( int i=0 ; i< 1 ; i++ ) { if( cursor.getColumnName(i) != null ) { try { if( cursor.getString(i) != null ) { //saving image to bitmap Bitmap bitmap = BitmapFactory.decodeFile(cursor.getString(cursor.getColumnIndex(DBHelper.PHOTO))); //converting it to base64 String en= encodeToBase64( resize(bitmap,1080,1920), Bitmap.CompressFormat.JPEG,50); Log.d("base",en); //inserting it to table pic mydb.insertpic(cursor.getInt(1),en); } } catch( Exception ignored) { } } } cursor.moveToNext(); } cursor.close(); mydb.updatebin(); return null; } protected void onPostExecute(String result) { } } 

How can I check his status from another action.?

+5
source share
3 answers

method 1:

make the Constance class and change the value of this class, and in another operation check that for the new value

method 2:

use Intent and put the data and value in extras , then call activity

method 3:

create and use interface listeners

method 4: (seems to be a good way)

use EventBus library: https://github.com/greenrobot/EventBus

anytime you need sen to notify of another action with:

  EventBus.getDefault().post("notify param"); 

then process the notification in another action:

  @Subscribe(threadMode = ThreadMode.MAIN) public void onMessageEvent("notify param") {/* Do something */}; 

(for more details on using EventBus see library documents on github )

+1
source

You can set the returned variable as "Public static" , from the current activity class, you can call the public static variable and display its value from the previous class.

Note. The value would decrease if the previous activity class was updated or destroyed, making its value equal to null, from which you will probably get a NullPointerException

0
source

Source: https://habr.com/ru/post/1262516/


All Articles