Android Google CloudMessaging API实现GCM客户端

Android Google CloudMessaging API实现GCM客户端,android,google-cloud-messaging,Android,Google Cloud Messaging,您好,我正在按照android的官方文档发送推送通知,因为旧方法已被弃用,但我在实现GCM客户端时面临问题 http://developer.android.com/google/gcm/client.html 教程写在这里,向下滚动查看 private void registerInBackground() 当我在我的应用程序上写这个函数时,它给了我这个错误 Syntax error on token "void", @ expected 我已经在谷歌上搜索过了,现在我知道了这个错误,因

您好,我正在按照android的官方文档发送推送通知,因为旧方法已被弃用,但我在实现GCM客户端时面临问题

http://developer.android.com/google/gcm/client.html
教程写在这里,向下滚动查看

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本身就在一个方法中,有什么帮助吗?

这是我为它找到的解决方案,令人失望的是,有33人查看了它,但没有人愿意帮忙。这是代码

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);
    }
private void registerInBackground(){
新建异步任务(){
@凌驾
受保护字符串doInBackground(无效…参数){
字符串msg=“”;
试一试{
如果(gcm==null){
gcm=GoogleCloudMessaging.getInstance(上下文);
}
regid=gcm.寄存器(发送方ID);
msg=“设备已注册,注册ID=“+regid;
//您应该通过HTTP将注册ID发送到服务器,以便
//可以使用GCM/HTTP或CCS向您的应用程序发送消息。
sendRegistrationIdToBackend();
//对于本演示:我们不需要发送它,因为设备将发送
//向服务器发送的上游消息,使用
//消息中的“发件人”地址。
//坚持注册-无需再次注册。
storeRegistrationId(上下文,regid);
}捕获(IOEX异常){
msg=“错误:”+ex.getMessage();
//如果出现错误,不要一直尝试注册。
//要求用户再次单击按钮,或执行以下操作
//退后。
}
返回味精;
}
@凌驾
受保护的void onPostExecute(字符串msg){
mDisplay.append(msg+“\n”);
}
}.执行(空,空,空);
}