0

我有 Android 应用程序,我使用了 GCM,我想显示设备注册 ID 窗口或在 log cat 上测试应用程序,我在 log cat 上看到模拟器有注册 ID 但我需要查看它如何???

此外,当我在我的手机上测试应用程序时,停止的项目...消息出现了,虽然它没有出现在模拟器上,当我评论这些权限时

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

这条消息消失了

班级

package com.example.elarabygroup;

import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gcm.GCMRegistrar;

import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;

public class GCMIntenetService extends GCMBaseIntentService {
    private static final String GCM_SENDER_ID = "1111111111";

    public GCMIntenetService() {
        super();
    }

    @Override
    protected void onRegistered(Context context, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        GCMRegistrar.setRegisteredOnServer(context, true);
    }

    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Log.i(TAG, "Device unregistered");
        if (GCMRegistrar.isRegisteredOnServer(context)) {
            String regId = "";
            Log.i(TAG, "unregistering device (regId = " + regId + ")");
            GCMRegistrar.setRegisteredOnServer(context, false);
        } else {
            // This callback results from the call to unregister made on
            // ServerUtilities when the registration to the server failed.
            Log.i(TAG, "Ignoring unregister callback");
        }
    }

    @Override
    protected void onError(Context context, String errorId) {
        // push error processing
    }

    @Override
    protected void onMessage(Context arg0, Intent arg1) {
        Log.i(TAG, "Received message");
        Log.i(TAG, "EXTRAS" + arg1.getExtras());
        //String message = getString(R.string.gcm_message);
        generateNotification(arg0, arg1.getStringExtra("Please download our new updates"));
        // notifies user about message

    }

    private void generateNotification(Context arg0, String stringExtra) {
        // TODO Auto-generated method stub

    }

    public static void registerInGCMService(Context context) {
        if (!checkIsGCMServiceAvailable(context)) {
            return;
        }
        final String regId = GCMRegistrar.getRegistrationId(context);
        if (regId.equals("")) {
            try {
                GCMRegistrar.register(context, GCM_SENDER_ID);
            } catch (Exception ex) {
            }
        } else {
            // Already registered
        }
    }

    public static boolean checkIsGCMServiceAvailable(Context context) {
        try {
            GCMRegistrar.checkDevice(context);
            GCMRegistrar.checkManifest(context);
            return true;
        } catch (Throwable th) {
            return false;
        }
    }

}
4

2 回答 2

1

Settings.Secure#ANDROID_ID将 Android ID 作为唯一的 64 位十六进制字符串返回。

import android.provider.Settings.Secure;

private String android_id = Secure.getString(getContext().getContentResolver(),
                                                        Secure.ANDROID_ID); 

android_id包含设备 ID。

作为示例,我仅将更改添加到 registerInGCMService() 方法。

public class GCMIntenetService extends GCMBaseIntentService {
    private static String GCM_SENDER_ID ;

    public GCMIntenetService() {
        super();
    }



    public static void registerInGCMService(Context context) {


         GCM_SENDER_ID =  Secure.getString(getContext().getContentResolver(),
                                                            Secure.ANDROID_ID); 

        if (!checkIsGCMServiceAvailable(context)) {
            return;
        }
        final String regId = GCMRegistrar.getRegistrationId(context);
        if (regId.equals("")) {
            try {
                GCMRegistrar.register(context, GCM_SENDER_ID);
            } catch (Exception ex) {
            }
        } else {
            // Already registered
        }
    }



}
于 2012-09-11T18:02:09.363 回答
0

您可以在您想要显示或弹出消息的活动中调用广播接收器,如 toast 。下面的代码做同样的事情

1)修改下面的onRegistered()方法

protected void onRegistered(Context context, String registrationId) 
{
        Log.i(TAG, "Device registered: regId = " + registrationId);
        GCMRegistrar.setRegisteredOnServer(context, true);
        CommonUtilities.displayMessage(context,registrationId);
}

2) 更改常用工具的 displayMessage(),如下所示

static void displayMessage (Context context, String message,String senderID,String recieverId,String Type) 

{
            Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
            intent.putExtra(EXTRA_MESSAGE, message);
            context.sendBroadcast(intent);
}

3)在编写主要活动时,我们制作了 Boradcaste 接收器,当应用程序向 gcm 注册时通知用户将代码放在主要活动中或您想要显示弹出窗口的位置

    @Override
        protected void onResume() {

            registerReceiver(mHandleMessageReceiver, new IntentFilter(
                    CommonUtilities.DISPLAY_MESSAGE_ACTION));

        }

    private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) 
            {
                        // you pop message display here when app. will get registered
Toast.makeText(context,getIntent().getExtras().getString(CommonUtilities.EXTRA_MESSAGE), 2).show();

            }
        };

    @Override
        protected void onStop() {
            // TODO Auto-generated method stub
            super.onPause();
            relaseMemory();
        }
于 2013-04-12T11:48:58.913 回答