调用GCMIntentService时出现问题,抛出异常:
java.lang.ClassCastException:com.test.test.GCMIntentService不能 强制转换为android.content.BroadcastReceiver
然而我的类确实扩展了GCMBaseIntentService,实际上我的构造“结束”很好(超级(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");
}
}
答案 0 :(得分:1)
考虑将GCMBroadcastReceiver传递给您将GCMIntentService传递给的任何内容。您不应该继承和覆盖名称,因为看起来您的意图服务已经the right name。
答案 1 :(得分:0)
该异常表示框架正在尝试访问您的GCMIntentService:
com.test.test.GCMIntentService
但您的源代码的包名称设置为:
package com.adk.test;
我认为将您的软件包名称更改为com.test.test
应该有效。
或者,如果您更喜欢坚持使用com.adk.test.GCMIntentService,那么请将您的AndroidManifest.xml的GCM接收器修改为:
<receiver android:name="com.adk.test.GCMIntentService" .... />