它没有从GCM服务器获取注册ID,应用程序没有给出任何错误,这是我的logcat。
07-13 16:43:39.920: I/** pushAndroidActivity **(5310): inside oncreate()
07-13 16:43:39.920: I/** pushAndroidActivity **(5310): started registration
07-13 16:43:39.920: D/GCMRegistrar(5310): resetting backoff for com.example.registration_id
07-13 16:43:39.920: V/GCMRegistrar(5310): Registering app com.example.registration_id of senders 803641917196
07-13 16:43:42.735: V/GCMBroadcastReceiver(5310): onReceive: com.google.android.c2dm.intent.REGISTRATION
07-13 16:43:42.735: V/GCMBroadcastReceiver(5310): GCM IntentService class: com.example.registration_id.GCMIntentService
07-13 16:43:42.735: V/GCMBaseIntentService(5310): Acquiring wakelock
这是主要的Activity .i使用现有的gcm.jar文件来使用GCMRegistrar类方法。
package com.example.registration_id;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.app.Activity;
import com.google.android.gcm.GCMRegistrar;
public class MainActivity extends Activity {
private String TAG = "** pushAndroidActivity **";
EditText edittext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "inside oncreate() ");
//check if registered,if not get registration id from GCM server
if(GCMRegistrar.getRegistrationId(this).equals(""))
{
Log.i(TAG, "started registration ");
GCMRegistrar.register(this, "803641917206");
}
else
{
final String regId = GCMRegistrar.getRegistrationId(this);
Log.i(TAG, "registration id ===== "+regId);
edittext=(EditText)findViewById(R.id.msg);
edittext.setText(regId);
}
}
}
这是服务类 -
package com.example.registration_id;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "===GCMIntentService===";
//default constructor
protected GCMIntentService(String senderId) {
super(senderId);
// TODO Auto-generated constructor stub
}
@Override
protected void onError(Context arg0, String arg1) {
// TODO Auto-generated method stub
Log.i(TAG, "error ");
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
}
@Override
protected void onRegistered(Context arg0, String registrationId) {
// TODO Auto-generated method stub
Log.i(TAG, "Device registered: regId = " + registrationId);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:4)
onReceive: com.google.android.c2dm.intent.REGISTRATION
正在调用onReceiver,这意味着您将收到一条消息。
protected GCMIntentService(String senderId) {
super(senderId); }
这是你的问题。您必须为GCMBaseIntentService子类声明一个public,no argument构造函数。否则,后台服务无法正确构造和使用GCMIntentService类。
为什么?
protected GCMIntentService(String senderID){
super(senderId);}
因为这个受保护的构造函数对于处理所有传入意图的服务类是不可见的,因为它位于不同的包中。
在您的GCMIntentService类中将senderId硬编码为静态常量,因为它将不再从Google更改。
答案 1 :(得分:1)
请注意:GCM IntentService类:com.example.registration_id.GCMIntentService
GCMIntentService类放在哪里(在哪个包中)?
检查清单文件以确保为GCMIntentService设置正确的包。
答案 2 :(得分:-1)
也检查此解决方案 将所有与GCM相关的类保留在主程序包中,以便进行注册。