在处理GCM时,我在“”类上遇到了一些错误。
错误出现在这里:

onHandleIntent(Intent intent)



  “无法从GCMBaseIntentService覆盖最终方法”


handleRegistration



  “类型中的方法handleRegistration(Context,Intent)
  GCMBaseIntentService不适用于参数(Intent)”


handleMessage



  “类型的方法handleMessage(Intent)未定义
  GCMIntenetService”


public final void onHandleIntent(Intent intent) {
            try {
                String action = intent.getAction();
                if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
                    handleRegistration(intent);
                } else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {
                    handleMessage(intent);
                }
            } finally {
                synchronized (LOCK) {
                    sWakeLock.release();
                }
            }
        }




package com.example.elarabygroup;

import com.google.android.gcm.GCMBaseIntentService;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;

public class GCMIntenetService extends GCMBaseIntentService {
    public static String TAG = "GCMIntentService";
    private static PowerManager.WakeLock sWakeLock;
    private static final Object LOCK = GCMIntenetService.class;

    /*Handling Intents sent by GCM*/
    static void runIntentInService(Context context, Intent intent) {
        synchronized (LOCK) {
            if (sWakeLock == null) {
                PowerManager pm = (PowerManager) context
                        .getSystemService(Context.POWER_SERVICE);
                sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                        "my_wakelock");
            }
        }
        sWakeLock.acquire();
        intent.setClassName(context, GCMIntenetService.class.getName());
        context.startService(intent);
    }



    public GCMIntenetService(String senderId) {
        super(senderId);
        // TODO Auto-generated constructor stub
        Log.d(TAG, "[GCMIntentService] start - sender Id : " + senderId);
    }

    @Override
    protected void onError(Context arg0, String arg1) {
        Log.d("onError", arg1);
    }

    @Override
    protected boolean onRecoverableError(Context context, String errorId) {
        Log.d("onRecoverableError", errorId);
        return false;
    }

    @Override
    /*
     * protected void onMessage(Context arg0, Intent arg1) { Log.d("onMessage",
     * String.valueOf(arg1)); }
     */
    protected void onMessage(Context arg0, Intent arg1) {

        Log.d("GCM", "RECIEVED A MESSAGE");
        // Get the data from intent and send to notificaion bar
        generateNotification(arg0, arg1.getStringExtra("**notificaion**"));
    }

    private void generateNotification(Context arg0, String stringExtra) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onRegistered(intent) {

    try {
        String action = intent.getAction();
        if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
            handleRegistration(intent);
        } else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {
            handleMessage(intent);
        }
    } finally {
        synchronized (LOCK) {
            sWakeLock.release();
        }
    }

}

    @Override
    protected void onUnregistered(Context arg0, String arg1) {
        Log.d("onUnregistered", arg1);
    }
}

最佳答案

您不应覆盖GCMBaseIntentService的最终方法。您仅需要ovveride此类基类的回调方法。这是一个例子:

public class GCMIntentService extends GCMBaseIntentService {

    private static final String GCM_SENDER_ID = "your_sender_id";

    public GCMIntentService() {
        super();
    }

    @Override
    protected void onRegistered(Context context, String gcmDeviceToken) {
        // remember and save somewhere "gcmDeviceToken"
    }

    @Override
    protected void onUnregistered(Context context, String s) {
        // Push unregistered processing
    }

    @Override
    protected void onError(Context context, String errorId) {
        // push error processing
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        // process Push message
    }

    public static void registerInGCMService(Context context) {
        if (!checkIsGCMServiceAvailable(context)) {
            return;
        }
        final String regId = GCMRegistrar.getRegistrationId(context);
        if (regId.equals("")) {
            try {
                GCMRegistrar.register(context, GCM_SENDER_ID);
            } catch (Exception ex) {
            }
        } else {
            // Already registered
        }
    }

    public static boolean checkIsGCMServiceAvailable(Context context) {
        try {
            GCMRegistrar.checkDevice(context);
            GCMRegistrar.checkManifest(context);
            return true;
        } catch (Throwable th) {
            return false;
        }
    }

}


更新:请注意-您应该使用自己的常量更改GCM_SENDER_ID常量的值,它应该是数字值,例如“ 1234567890123”

关于android - 处理GCM发送的 Intent 时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12370440/

10-09 15:58