2013-12-19 36 views
6

我遇到了一些奇怪的警告與asynctask.I'm學習GCM,所以我嘗試使用asynctask。 這是警告:Asynctask奇怪的行爲

Type safety: The method execute(Object...) belongs to the raw type AsyncTask. References to generic type AsyncTask<Params,Progress,Result> should be parameterized 

我想了解的這個warning.IS它有關線程。爲什麼它不是安全的意義?這是什麼原料?

這是我的代碼:

private void registerInBackground() { 
    // TODO Auto-generated method stub 



    new AsyncTask() { 
     @Override 
     protected Object doInBackground(Object... arg0) { 
      // TODO Auto-generated method stub 
      String msg = ""; 
      try { 
       if (gcm == null) { 
        gcm = GoogleCloudMessaging.getInstance(getApplicationContext()); 
       } 
       regid = gcm.register(SENDER_ID); 
       msg = "Device registered, registration ID=" + regid; 

       // You should send the registration ID to your server over HTTP, 
       // so it can use GCM/HTTP or CCS to send messages to your app. 
       // The request to your server should be authenticated if your app 
       // is using accounts. 
       sendRegistrationIdToBackend(); 

       // For this demo: we don't need to send it because the device 
       // will send upstream messages to a server that echo back the 
       // message using the 'from' address in the message. 

       // Persist the regID - no need to register again. 
       storeRegistrationId(getApplicationContext(), regid); 
      } catch (IOException ex) { 
       msg = "Error :" + ex.getMessage(); 
       // If there is an error, don't just keep trying to register. 
       // Require the user to click a button again, or perform 
       // exponential back-off. 
      } 
      return msg; 
     } @Override 
     protected void onPostExecute(Object result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 
      Toast.makeText(getApplicationContext(), ""+result.toString(), Toast.LENGTH_LONG).show(); 
     } 
    }.execute(null, null, null); 



} 

感謝

回答

3

的AsyncTask是generic Class可以參數化。警告意味着您應該明確指定參數。

AsyncTask被定義爲AsyncTask<Params,Progress,Result>,它需要3個參數Params,ProgressResult可以是任何類型/類。

在上面的代碼中,您返回一個字符串doInBackground()。返回值doInBackground()AsyncTaskResult)的第三個參數。

爲了使警告消失,實現您的AsyncTask這樣的:

new AsyncTask<Void, Void, String>() { 
     @Override 
     protected String doInBackground(Void... voids) { 
      String message = ""; 

      //do something 

      return message; 
     } 

     @Override 
     protected void onPostExecute(String result) { 

     } 
    }.execute(); 
+0

現在的工作。 非常感謝 –

0

它做仿製藥。 AsyncTask課程的打字是AsyncTask<Params, Progress, Result>。你得到的信息是因爲你在做new AsyncTask()。你應該做的是類似new AsyncTask<Void, Void, Void>(),因爲你不打算使用這些類型。

該API參考包含有關該主題的更多信息。 enter link description here

1

通常使用AsyncTask /使用參數實例化... AsyncTask<Params, Progress, Result>。什麼參數表示爲:

  • PARAMS:
  • 他們是你傳遞到 doInBackground() Objects。例如,如果我將 Params初始化爲 String,則現在可以通過我的 doInBackground(String... params)中的 params[0], params[1]等訪問字符串參數。這是您指定的最重要的參數之一,因爲在調用 AsyncTask時,您可以發送在 doInBackground線程中需要的參數。像新的一樣 AsyncTask().execute(string);

  • 進展:
  • 當使用 AsyncTask一個通常佔據活動加載內容等,您可以使用進度參數給上下文用戶的事情會應該的樣子。假設您阻止了用戶界面,告訴用戶它正在下載內容。有了這一進展,您可以指定已下載了多少內容。例如,如果此參數是 integer,我可以調用 doInBackground線程中的 publishProgress()來顯示當前正在下載的內容的進度。

  • 結果:
  • 通常這是允許與用戶界面進行交互的唯一參數。假設您已完成下載您的內容。並且想要指定操作完成,顯示成功消息。這是你做的地方。例如,您想返回 String,表示下載完成。在你的 doInBackground最後你的線程你 return string;。該值因此傳遞到 onPostExecute(String result)。從那裏你可以設置 textView.setText(result)

    基本上的AsyncTask是基於GENERICSVARARGS(Java概念)主要

    GENERICS暗示在AsyncTask參數可以是任何類型的和可變參數指定了可以在多個值發送這些參數內。

    在你的情況下,由於你沒有做任何重要的改變UI,你可以撥打​​。爲了更好地瞭解AsyncTask的工作情況,您可以看到以下example

    這是一個漫長讀,但希望這澄清了事情:)