Extending AsyncTask

前端 未结 4 1780
礼貌的吻别
礼貌的吻别 2020-12-13 13:41

In my android app I am performing some operations in the doInBackground by extending AsyncTask class. (I have no use in per

相关标签:
4条回答
  • 2020-12-13 14:02

    The AsyncTask class can be thought of as a very convenient threading mechanism. It gives you a few tools that you can use that simple Java threads simply don't have such as on cancel cleanup operations. You don't have to do any UI in the background. You could simply execute one by writing one as an anonymous class like this:

        new AsyncTask<Integer, Void, Void>(){
            @Override
            protected Void doInBackground(Integer... params) {
                // **Code**
                return null;
            }
        }.execute(1, 2, 3, 4, 5);
    

    It will execute whatever you put in doInBackground on a background thread with the given parameters. Likewise, you can simply use Void and execute with no parameters.

    The only advantage I could think of executing a thread this way would be to aid in future maintenance. There might be a case where you want to modify certain things that are required to be on the UI thread, in which case you would override the other methods. Other cases would be you simply don't do the action enough to justify writing out another class, so just create one on the fly and be done with it.

    EDIT:

    To answer #3: they're effectively the same. The Void object is a Java object just like anything else. You're not using Void, so what you use in it's place doesn't matter. It's just the AsyncTask contract requires three class types to be passed in, and by default they're Object which is the baseline class of everything.

    0 讨论(0)
  • 2020-12-13 14:04
    1. is this proper use of asynctask?

    I don't think there is any proper use. You can use asynctask to perform intensive operations on background thread and publish result, updates on main thread. It's not mandatory that you must publish result or progress. That's given to you. Your decision.

    But for your network operations, you can use thread also. But later you decided to publish result and progress on main thread, Asynctask will help you in a simple way.

    Threads don't.

    Asynctask uses generic datatypes as parameters. You must provide it. If you don't want to provide or accept any data from asyctask. You can use Void type,

    just like given below

    class MessagePooling extends AsyncTask<Void,Void,Void>
    

    It means asynctask will get no parameters, give you no progress or result.

    For more about asynctask: http://androidride.com/beginners-guide-asynctask-android-tutorial-example/

    0 讨论(0)
  • 2020-12-13 14:08

    (I have no use in performing any UI in this class)

    Then just use the normal Thread class rather than using AsyncTask class which is designed for dealing with UI changes during the thread's life time:

    Runnable r = new Runnable()
    {
        @Override
        public void run()
        {
            // your code here
        }
    };
    
    Thread t = new Thread(r);
    t.start();
    
    0 讨论(0)
  • 2020-12-13 14:13

    Is this proper use of AsyncTask ?

    You don't said what is your goal. But AsyncTask is generally used for long tasks and updating UI during work exactly to notify some progress change of some long work. It's also very efficient, type-safe tool and more complex than Handler for example.

    What is the difference between extending AsyncTask and AsyncTask

    AsyncTask is generic type and you should use generics with them is code more faster, more type-safe.

    I have no use in performing any UI

    So you rather can use Handler or classical Runnable interface and you can work only with worker threads basically.


    Note: Generally, using of generics types makes code faster, most type-safe. Also next advantage is strong type checking at compile source code, restrictions of explicit conversions etc.

    0 讨论(0)
提交回复
热议问题