android - 使用 Asynctask 进行 GCM 注册时出错

标签 android

我正在使用来自开发者网站的相同代码,但在编译时出错。 http://developer.android.com/google/gcm/gs.html

复制下面的代码

private void registerBackground() {
new AsyncTask() {
    @Override
    protected String doInBackground(Void... params) {
        String msg = "";
        try {
            regid = gcm.register(GCM_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 will echo back 
            // the message using the 'from' address in the message. 

            // Save the regid for future use - no need to register again.
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString(PROPERTY_REG_ID, regid);
            editor.commit();
        } catch (IOException ex) {
            msg = "Error :" + ex.getMessage();
        }
        return msg;
    }
    // Once registration is done, display the registration status
    // string in the Activity's UI.
    @Override
    protected void onPostExecute(String msg) {
        mDisplay.append(msg + "\n");
    }
}.execute(null, null, null);
}

我在编译时收到错误消息“Asynctask 是一种原始类型。对泛型类型的引用应该被参数化。

最佳答案

您还没有声明泛型类型参数。

改变

new AsyncTask() {

new AsyncTask<Void,Void,String>() {

还有,

execute(null, null, null);

可以改成

execute();

关于android - 使用 Asynctask 进行 GCM 注册时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16762228/

相关文章:

android - 为什么即使使用width=device-width,android浏览器视口(viewport)也比手机实际屏幕尺寸小很多?

android - 无法使用gradle build dystem将生成的.aar文件上传到android studio中的本地Maven存储库

android - 是否可以通过编程方式检测是真实的还是虚拟的 Android 设备?

java - 错误: Invalid start tag LinearLayout

android - 光标经过表情符号

安卓可扩展列表

android - Xposed Framework如何在Android中 Hook 方法

android - 使用 tabhost 时 WebView 无法正确填充屏幕

android - 如何从 Firebase 接收具有特定字符串 android 的数据

android - 为什么 NotificationManagerCompat::cancelAll() 得到 SecurityException?