Android的 - 从GCM设备ID返回空 [英] Android - Device ID from GCM return empty

查看:251
本文介绍了Android的 - 从GCM设备ID返回空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不从GCM获得唯一的设备ID为进一步使用。我尝试所有,但结果是否定的,设备ID我得到的是空的。请帮助

下面是code:

  GCMRegistrar.checkDevice(本);
    GCMRegistrar.checkManifest(本);    的setContentView(R.layout.cmain);
    填充mDisplay =(的TextView)findViewById(R.id.display);    最后弦乐REGID = GCMRegistrar.getRegistrationId(本);
    Log.i(TAG,注册ID =====+ REGID);    如果(regId.equals()){
        GCMRegistrar.register(这一点,CommonUtilities.SENDER_ID);
    }其他{
        Log.v(TAG,已经注册);
    }

logcat的结果:

  I / pushAndroidActivity(2295):注册ID =====
D / GCMRegistrar(2295):为shane.nuke重置退避
V / GCMRegistrar(2295):发件人510772122772的注册应用shane.nuke
V / GCMBroadcastReceiver(2295)的onReceive:com.google.android.c2dm.intent.REGISTRATION
V / GCMBroadcastReceiver(2295):GCM IntentService类:shane.nuke.GCMIntentService
V / GCMBaseIntentService(2295):获取激活锁定

下面是code为GCMIntentService_2

 公共类GCMIntentService_2扩展GCMBaseIntentService {公共GCMIntentService_2(){
    超(CommonUtilities.SENDER_ID);
}
私有静态最后弦乐TAG ==== GCMIntentService ===
@覆盖
保护无效onRegistered(上下文为arg0,弦乐registrationId){
    Log.i(TAG,设备注册:REGID =+ registrationId);
    Log.e(TAG,设备注册:REGID =+ registrationId);
}
@覆盖
保护无效onUnregistered(上下文为arg0,ARG1字符串){
    Log.i(TAG未注册=+ ARG1);
}
@覆盖
保护无效的onMessage(上下文为arg0,ARG1意向){
    Log.i(TAG,新的消息=);
}
@覆盖
保护无效的onError(上下文为arg0,弦乐ErrorID中){
    Log.i(TAG,收到的错误:+ ErrorID中);
}
@覆盖
保护布尔onRecoverableError(上下文的背景下,字符串ErrorID中){
    返回super.onRecoverableError(背景下,ErrorID中);
}

}

下面是Mainfest.xml code:

 <清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=shane.nuke
安卓版code =1
机器人:=的versionName1.0><用途-SDK
    安卓的minSdkVersion =12
    机器人:targetSdkVersion =23/><许可
    机器人:名字=shane.nuke.permission.C2D_MESSAGE
    安卓的ProtectionLevel =签名/><使用许可权的android:NAME =shane.nuke.permission.C2D_MESSAGE/>
<使用许可权的android:NAME =android.permission.GET_ACCOUNTS/>
<使用许可权的android:NAME =android.permission.WAKE_LOCK/>
<使用许可权的android:NAME =android.permission.INTERNET对/>
<使用许可权的android:NAME =com.google.android.c2dm.permission.RECEIVE/><应用
    机器人:allowBackup =真
    机器人:图标=@绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>
    <活动
        机器人:名字=shane.nuke.second.Push
        机器人:标签=@字符串/ APP_NAME>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>            <类机器人:名字=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
    <活动机器人:名字=shane.nuke.next.GCMMessageView>
    < /活性GT;    <接收
        机器人:名字=com.google.android.gcm.GCMBroadcastReceiver
        机器人:权限=com.google.android.c2dm.permission.SEND>
        &所述;意图滤光器>
            <作用机器人:名字=com.google.android.c2dm.intent.RECEIVE/>
            <作用机器人:名字=com.google.android.c2dm.intent.REGISTRATION/>            <类机器人:名字=shane.nuke/>
        &所述; /意图滤光器>
    < /接收器>
    <服务机器人:名字=shane.nuke.second.GCMIntentService_2/>    &所述;元数据
        机器人:名字=com.google.android.gms.version
        机器人:值=@整数/ GOOGLE_PLAY_SERVICES_VERSION/>
< /用途>


解决方案

尽量把所有文件主包和使用移动数据网络。有时WiFi未的作品。

希望这有助于你。

I have to get only device id from GCM for further use. I try all but result is negative, Device id I get is empty. Please help

Here is code:

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 results :

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

Here is code for 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);
}

}

Here is Mainfest.xml code:

<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>

解决方案

try to put all files in main package and use mobile data for internet. Sometime wifi not works.

Hope this helps you.

这篇关于Android的 - 从GCM设备ID返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆