2013-09-28 60 views
3

你好,我是下了Android的官方文檔發送推送通知的老方法已經過時,但在執行的GCM客戶Google雲端通訊的API實現GCM客戶

http://developer.android.com/google/gcm/client.html 

教程寫在這裏面臨的問題IM,滾動往下看吧

private void registerInBackground() 

當我寫我的應用程序這個功能它給了我這個錯誤

Syntax error on token "void", @ expected 

我已經使用了它,我知道現在錯誤,這種方法試圖創建一個方法內的方法,但仍然我很困惑,因爲它是官方文檔我必須做錯了什麼,任何人都可以指出PLZ? 這裏是這個教程的方法:

private void registerInBackground() { 
    new AsyncTask() { 
     @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. 
       // 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(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; 
     } 

     @Override 
     protected void onPostExecute(String msg) { 
      mDisplay.append(msg + "\n"); 
     } 
    }.execute(null, null, null); 
    ... 
    /** 
    * Sends the registration ID to your server over HTTP, so it can use GCM/HTTP 
    * or CCS to send messages to your app. Not needed for this demo since the 
    * device sends upstream messages to a server that echoes back the message 
    * using the 'from' address in the message. 
    */ 
    private void sendRegistrationIdToBackend() { 
     // Your implementation here. 
    } 
} 

現在看到sendRegistrationIdToBackend是一種方法本身,任何幫助PLZ裏面?

回答

11

這裏是解決方案,我發現它和失望的是,33人觀看,但沒有人理會ANS反正這裏是代碼

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. 
        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(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; 
      } 

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