在android studio中使用GCMRegistrar

时间:2015-10-29 09:35:08

标签: android android-studio google-cloud-messaging

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 17
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile project(':jLibUtils')
    compile files('libs/androidmarketapi-0.6.jar')
    compile files('libs/jsoup-1.7.3.jar')
    compile 'com.google.android.gms:play-services:+'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.google.android.gms:play-services-gcm:8.1.0'
}

此外,我想编写"导入com.google.android.gcm.GCMRegistrar"。如果我写源" import com。" ,没有谷歌选项。我无法找到并写下

如何在android studio中使用这些gcm类??

1 个答案:

答案 0 :(得分:1)

使用Android中的最新GCM仅使用以下代码或类在您的应用程序中注册GCM。

package com.bhadresh.Activity;

import android.app.IntentService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;
import com.bhadresh.Other.Constant;
import com.bhadresh.R;

/**
 * Created by windws on 07-Jul-15.
 */
public class RegistrationIntentService extends IntentService {
    private static final String TAG = "RegistrationIntentService.class.getSimpleName();";


    public RegistrationIntentService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        try {
            synchronized (TAG) {
                InstanceID instanceID = InstanceID.getInstance(this);
                String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
                SharedPreferences settings =
                        getSharedPreferences("" + Constant.PREFRENCE_NAME, MODE_PRIVATE);
                SharedPreferences.Editor editor = settings.edit();
               // Log.d("device token", token);
                editor.putString("deviceToken", token);
                editor.commit();

            }
        } catch (Exception e) {

        }
        // Notify UI that registration has completed, so the progress indicator can be hidden.
        // LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(getString(R.string.intent_name_REGISTRATION_COMPLETE)));
    }
}