Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中asyntask参数的区别是什么?_Android_Android Asynctask - Fatal编程技术网

android中asyntask参数的区别是什么?

android中asyntask参数的区别是什么?,android,android-asynctask,Android,Android Asynctask,正如我的问题所说,我指的是 它告诉我 Asyntask <params, progress, result> Asyntask 但我没有使用进步。它的状态符合你的安排吗?还是有规则 例如: class loadingdata extends AsyncTask<?,?,?> protected void onPreExecute() {} protected String doInBackground(String... args) {}

正如我的问题所说,我指的是

它告诉我

  Asyntask <params, progress, result> 
Asyntask
但我没有使用进步。它的状态符合你的安排吗?还是有规则

例如:

   class loadingdata extends AsyncTask<?,?,?>
   protected void onPreExecute() {}
   protected String doInBackground(String... args) {} 
   protected void onPostExecute() {}
类加载数据扩展异步任务
受保护的void onPreExecute(){}
受保护的字符串doInBackground(字符串…args){}
受保护的void onPostExecute(){}
那么,我是否应该将3参数作为

asyntask <void String void> ? 
asyntask?
或者它有一个规则

<preExecute, postExecute, doInBackground> or so fourth?
大约是第四名?
请帮助我,我是这方面的初学者,我不理解它。

因为AsyncTask有通用参数,所以您必须提供所有参数。如果您不使用任何类型,那么您提供的类型实际上并不重要。

这些类型是泛型的,因此您可以使用任何类或基元类型。当您希望在AsyncTasks中执行该语句时,请通过生成该语句的实例来使用该语句,然后为该实例调用execute()

后藤:

有关AsyncTask的更多详细使用信息。

AsyncTask的泛型类型

异步任务使用的三种类型如下:

 Params -> the type of the parameters sent to the task upon execution.
 Progress -> the type of the progress units published during the background computation.
 Result -> the type of the result of the background computation.
  • Params,执行时发送到任务的参数类型

  • 进度,后台计算期间发布的进度单位的类型

  • 结果,背景计算结果的类型

  • 异步任务并不总是使用所有类型要将类型标记为未使用,只需使用类型Void:

     1. Instance of Async Task needs to be created in UI thread. As shown in  onClick method a new instance of LongOperation is created there. Also execute method with parameters should be called from UI thread.
    
      2. Methods onPostExecute, onPreExecute and onProgressUpdate  should not be explicitly called.
    
     3. Task can be executed only once.
    
    私有类MyTask扩展了AsyncTask{…}

    范例

        private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
        protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }
    
     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }
    
     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
     }
    
     URL parameter for doInBackground(URL... urls)
     You call publishProgress((int)somevalue) in doinBackground() to update progress.
     Integer parameter for onProgressUpdate(Integer... progress)  
     Long (result) parameter for onPostExecute(). The result is received from doInBackground().    
    

    如果不需要一个或多个
    AsyncTask
    参数,请使用
    Void
    (请注意大写字母V,因此它是
    Void
    ,而不是
    Void

    AsyncTask
    的签名中,第一个是传递给
    doInBackground()
    的数组类型,第二个是调用
    publishProgress()
    publishProgress()
    时使用的数组类型,第三个是
    doinbackdrond()返回的数据类型
    并传递到
    onPostExecute()

    例如

    private class MyAsyncTask extends AsyncTask<String, Integer, Boolean>
    

    异步任务由3种通用类型定义,称为
    Params、Progress和Result
    ,以及4个步骤,分别称为
    onPreExecute
    doInBackground
    onProgressUpdate
    onPostExecute

    AsyncTask的泛型类型:

    异步任务使用的三种类型如下:

     Params -> the type of the parameters sent to the task upon execution.
     Progress -> the type of the progress units published during the background computation.
     Result -> the type of the result of the background computation.
    
    异步任务并不总是使用所有类型。要将类型标记为未使用,只需使用类型Void:

      private class MyTask extends AsyncTask<Void, Void, Void> { ... }
    
    要记住的要点:

     1. Instance of Async Task needs to be created in UI thread. As shown in  onClick method a new instance of LongOperation is created there. Also execute method with parameters should be called from UI thread.
    
      2. Methods onPostExecute, onPreExecute and onProgressUpdate  should not be explicitly called.
    
     3. Task can be executed only once.
    
    让我们看一个示例类LongOperation,它扩展了下面的异步任务:查看源打印

       private class LongOperation extends AsyncTask<String, Void, String> {
       @Override
       protected String doInBackground(String... params) {
              // perform long running operation operation
              return null;
       }
       /* (non-Javadoc)
       * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
       */
       @Override
       protected void onPostExecute(String result) {
              // execution of result of Long time consuming operation
       }
       /* (non-Javadoc)
       * @see android.os.AsyncTask#onPreExecute()
       */
       @Override
       protected void onPreExecute() {
       // Things to be done before execution of long running operation. 
       //For example showing ProgessDialog
       }
       /* (non-Javadoc)
       * @see android.os.AsyncTask#onProgressUpdate(Progress[])
       */
       @Override
       protected void onProgressUpdate(Void... values) {
              /* Things to be done while execution of long running operation 
              is in progress.
              For example updating ProgessDialog */
              }
       }
    
    私有类LongOperation扩展了异步任务{
    @凌驾
    受保护的字符串doInBackground(字符串…参数){
    //执行长时间运行操作
    返回null;
    }
    /*(非Javadoc)
    *@see android.os.AsyncTask#onPostExecute(java.lang.Object)
    */
    @凌驾
    受保护的void onPostExecute(字符串结果){
    //执行耗时较长的操作的结果
    }
    /*(非Javadoc)
    *@see android.os.AsyncTask#onPreExecute()
    */
    @凌驾
    受保护的void onPreExecute(){
    //在执行长时间运行操作之前要做的事情。
    //例如,显示ProgesDialog
    }
    /*(非Javadoc)
    *@请参阅android.os.AsyncTask#onProgressUpdate(Progress[])
    */
    @凌驾
    受保护的void onProgressUpdate(void…值){
    /*执行长时间运行操作时要做的事情
    正在进行中。
    例如,更新ProgesDialog*/
    }
    }
    
    但是我怎么知道在什么情况下应该采取什么样的措施呢。。或者我可以简单地说,在任何情况下,任何其他2将是无效的?它会自动接受吗?@user2198192您使用AsyncTask想要完成什么?
       private class LongOperation extends AsyncTask<String, Void, String> {
       @Override
       protected String doInBackground(String... params) {
              // perform long running operation operation
              return null;
       }
       /* (non-Javadoc)
       * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
       */
       @Override
       protected void onPostExecute(String result) {
              // execution of result of Long time consuming operation
       }
       /* (non-Javadoc)
       * @see android.os.AsyncTask#onPreExecute()
       */
       @Override
       protected void onPreExecute() {
       // Things to be done before execution of long running operation. 
       //For example showing ProgessDialog
       }
       /* (non-Javadoc)
       * @see android.os.AsyncTask#onProgressUpdate(Progress[])
       */
       @Override
       protected void onProgressUpdate(Void... values) {
              /* Things to be done while execution of long running operation 
              is in progress.
              For example updating ProgessDialog */
              }
       }