How to check if Async Task is already running

后端 未结 4 529
一个人的身影
一个人的身影 2020-12-30 19:52

I have an app that needs to do an intensive database operation on start up. The app holds a local copy of the contacts on the phone and synchronizes with the android contact

相关标签:
4条回答
  • 2020-12-30 20:26

    Use getStatus() to get the status of your AsyncTask. If status is AsyncTask.Status.RUNNING then your task is running.

    EDIT: you should reconsider your implementation and hold the AsyncTask probably in a Service or IntentService to fetch your data from the web.

    0 讨论(0)
  • 2020-12-30 20:32

    Yes Right guys these are some of the examples.

    LoadMusicInBackground lmib = new LoadMusicInBackground();
    
    if(lmib.getStatus() == AsyncTask.Status.PENDING){
        // My AsyncTask has not started yet
    }
    
    if(lmib.getStatus() == AsyncTask.Status.RUNNING){
        // My AsyncTask is currently doing work in doInBackground()
    }
    
    if(lmib.getStatus() == AsyncTask.Status.FINISHED){
        // My AsyncTask is done and onPostExecute was called
    }
    
    0 讨论(0)
  • 2020-12-30 20:35

    I think you should check the concept of Application in Android. http://developer.android.com/reference/android/app/Application.html

    In fact there is no such thing as

    different instance of the app

    . The Application is always the same for all your Activities/Services. That means that you'd left the Activity and opened it again, 2 cases are possible:

    1. The system already killed your application. In this case AsyncTask is dead already and it's safe to start a new one
    2. The Application was still alive, so AsyncTask possibly still running.

    In 2nd case I will recommend to use some static variables, pointing to this AsyncTask or it's state. If your app was still alive when 2nd time opened - all static references will be still valid, so you can successfully operate.

    PS: By the way, in current approach be aware that your application can be terminated by the system at any time. So AsyncTask can be interrupted in any moment. It it's not ok for you - please check IntentServices - components, specially designed for background-operation purpose. http://developer.android.com/reference/android/app/IntentService.html

    Good luck!

    0 讨论(0)
  • 2020-12-30 20:35

    I've managed to handle this problem with some sort of Singleton pattern. Hope it helps.

    // fill the places database from a JSON object
    public class myAsyncTask extends AsyncTask<Void,Integer,Integer> {
    
        Activity mContext = null;
        static AsyncTask<Void,Integer,Integer> myAsyncTaskInstance = null; 
    
        // Private Constructor: can't be called from outside this class
        private myAsyncTask(Activity iContext) {
            mContext = iContext; 
        }
    
        public static AsyncTask<Void, Integer, Integer> getInstance(Activity iContext) {
            // if the current async task is already running, return null: no new async task 
            // shall be created if an instance is already running
            if (myAsyncTaskInstance != null && myAsyncTaskInstance.getStatus() == Status.RUNNING) {
                // it can be running but cancelled, in that case, return a new instance
                if (myAsyncTaskInstance.isCancelled()) {
                    myAsyncTaskInstance = new myAsyncTask(iContext);
                } else {
                    // display a toast to say "try later"
                    Toast.makeText(iContext, "A task is already running, try later", Toast.LENGTH_SHORT).show();    
    
                    return null;
                }
            }
    
            //if the current async task is pending, it can be executed return this instance
            if (myAsyncTaskInstance != null && myAsyncTaskInstance.getStatus() == Status.PENDING) {
                return myAsyncTaskInstance;
            }
    
            //if the current async task is finished, it can't be executed another time, so return a new instance
            if (myAsyncTaskInstance != null && myAsyncTaskInstance.getStatus() == Status.FINISHED) {
                myAsyncTaskInstance = new myAsyncTask(iContext);
            }
    
    
            // if the current async task is null, create a new instance
            if (myAsyncTaskInstance == null) {
                myAsyncTaskInstance = new myAsyncTask(iContext);
            }
            // return the current instance
            return myAsyncTaskInstance;
        }
    
        @Override
        protected Integer doInBackground(Void... iUnUsed) {
            // ...
        }
    }
    
    0 讨论(0)
提交回复
热议问题