调用 GCMIntentService 时出现问题,抛出异常:
java.lang.ClassCastException:com.test.test.GCMIntentService 无法转换为 android.content.BroadcastReceiver
然而,我的类确实扩展了 GCMBaseIntentService,实际上我的构造“结束”得很好(super(SENDER_ID); 传递没有问题)并且问题出现在退出构造函数时,我怀疑当内部类尝试强制转换新创建的实例时我的扩展课。
如果需要,供参考的代码:
package com.adk.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Notification;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import utilities.Logd;
import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gcm.GCMConstants;
import com.google.android.gcm.GCMRegistrar;
public class GCMIntentService extends GCMBaseIntentService {
private final String LOG_TAG = "Test";
private final static String senderID = "66610078X85X";
public GCMIntentService(){
super("66610078X85X");
Logd.i(LOG_TAG, "GCM passed");
}
@Override
protected void onError(Context arg0, String errorID) {
Logd.e(LOG_TAG, errorID, null);
}
@Override
protected void onMessage(Context arg0, Intent intent) {
enviarNotificacion(arg0, intent);
}
@Override
protected void onRegistered(Context arg0, String deviceID) {
Registrar(arg0, deviceID);
Logd.i(LOG_TAG, "Registered");
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
Logd.i(LOG_TAG, "Unregistered");
}
}