2013-07-15 31 views
3

在創建GCM客戶端應用程序時,asynctask正在給出編譯錯誤。 OnCreate我們正在調用registerBackgrouod,它將檢查gcm實例是否正在運行,如果沒有創建一個。創建GCM客戶端應用程序,asynctask失敗

但asyntask是給錯誤:「的AsyncTask不能被解析爲一個類型」

private void registerBackground() { 
    new AsyncTask() { 
     protected String doInBackground(Void... params) { 
      String msg = ""; 
      try { 
       if (gcm == null) { 
        gcm = GoogleCloudMessaging.getInstance(context); 
       } 
       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. 
       // 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. 

       // Save the regid - no need to register again. 
       setRegistrationId(context, regid); 
      } catch (IOException ex) { 
       msg = "Error :" + ex.getMessage(); 
      } 
      return msg; 
     } 


     protected void onPostExecute(String msg) { 
      mDisplay.append(msg + "\n"); 
     } 
    }.execute(null, null, null); 
+1

應該是'新的AsyncTask <太虛,太虛, String>()' – AlexBcn

+0

它沒有工作,使用線程和thread.start()而不是asynctask工作。 – John

+0

Android文檔已損壞。跟蹤器中有一個錯誤報告,但我不知道他們什麼時候會修復它。 – Xeos

回答

-1

這是因爲,則params的你傳遞給異步任務。 對於進一步的幫助: 我最近上傳的功能齊全的GCM Java客戶端,以我的Github上的帳戶: GCM Android Client

它得到了服務器和客戶端實現。

0

正如AlexBcn所觀察到的,並且根據AsyncTask的文檔,您將傳遞給AsyncTask三種類型作爲參數。因爲要返回GCM推送通知的有效載荷爲一個字符串,你會調用AsyncTask<Void, Void, String>

所以GCM客戶端的正確的代碼片段是:

private void registerInBackground() { 
     new AsyncTask<Void, Void, String>() { 
     @Override 
     protected String doInBackground(Void... params) { 
      String msg = ""; 
      try { 
       if (gcm == null) { 
        gcm = GoogleCloudMessaging.getInstance(context); 
       } 
       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. 
       // 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(context, 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; 
     }.execute(null, null, null); 
    } 
相關問題