Android - 来自 GCM 的设备 ID return 为空

Android - Device ID from GCM return empty

我只能从 GCM 获取设备 ID 以供进一步使用。我尝试了所有但结果是否定的,我得到的设备 ID 是空的。请帮忙

这是代码:

GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);

    setContentView(R.layout.cmain);
    mDisplay = (TextView) findViewById(R.id.display);

    final String regId = GCMRegistrar.getRegistrationId(this);
    Log.i(TAG, "registration id =====  " + regId);

    if (regId.equals("")) {
        GCMRegistrar.register(this, CommonUtilities.SENDER_ID);
    } else {
        Log.v(TAG, "Already registered");
    }

Logcat 结果:

I/ pushAndroidActivity (2295): registration id =====  
D/GCMRegistrar(2295): resetting backoff for shane.nuke
V/GCMRegistrar(2295): Registering app shane.nuke of senders 510772122772
V/GCMBroadcastReceiver(2295): onReceive: com.google.android.c2dm.intent.REGISTRATION
V/GCMBroadcastReceiver(2295): GCM IntentService class: shane.nuke.GCMIntentService
V/GCMBaseIntentService(2295): Acquiring wakelock

这是 GCMIntentService_2

的代码
public class GCMIntentService_2 extends GCMBaseIntentService {

public GCMIntentService_2() {
    super(CommonUtilities.SENDER_ID);
}
private static final String TAG = "===GCMIntentService===";
@Override
protected void onRegistered(Context arg0, String registrationId) {
    Log.i(TAG, "Device registered: regId = " + registrationId);
    Log.e(TAG, "Device registered: regId = " + registrationId);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
    Log.i(TAG, "unregistered = " + arg1);
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
    Log.i(TAG, "new message= ");
}
@Override
protected void onError(Context arg0, String errorId) {
    Log.i(TAG, "Received error: " + errorId);
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
    return super.onRecoverableError(context, errorId);
}

}

这里是 Mainfest.xml 代码:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="shane.nuke"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="12"
    android:targetSdkVersion="23" />

<permission
    android:name="shane.nuke.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="shane.nuke.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="shane.nuke.second.Push"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="shane.nuke.next.GCMMessageView" >
    </activity>

    <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="shane.nuke" />
        </intent-filter>
    </receiver>


    <service android:name="shane.nuke.second.GCMIntentService_2" />

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>

将您的 GCM 代码放入应用程序包中

将您的 GCM 代码放入清单中定义的应用程序的主包(例如 "com.sample" 在本例中):

包="com.sample"

GCMRegister 已弃用 Google 云消息传递参考此 link https://developers.google.com/cloud-messaging/android/start.

尝试将所有文​​件放在主包中并使用移动数据上网。有时 wifi 不能用。

希望对您有所帮助。