0
我正在嘗試使用Implementing GCM Client獲取GCM
設備註冊標識。我使用了一個類Functions
來實現這個過程。這裏是我的Android GCM:檢索設備註冊標識
public class Functions {
public Functions(Activity activity) {
// TODO Auto-generated constructor stub
this.activity = activity;
this.prefs = activity.getSharedPreferences(activity.getClass().getSimpleName(), Context.MODE_PRIVATE);
}
public void deviceRegistrationID(){
if (checkPlayServices()) {
gcm = GoogleCloudMessaging.getInstance(activity);
regid = getRegistrationId(activity);
if (regid.isEmpty()) {
registerInBackground();
}
} else {
Log.i(TAG, "No valid Google Play Services APK found.");
}
}
public String getGCMRegistrationID(){
String registeredID = prefs.getString(PROPERTY_REG_ID, "");
System.out.println("The REG ID:"+prefs.getString(PROPERTY_REG_ID, ""));
return registeredID;
}
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(activity);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, activity,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
activity.finish();
}
return false;
}
return true;
}
private String getRegistrationId(Context context) {
String registrationId = prefs.getString(PROPERTY_REG_ID, "");
if (registrationId.isEmpty()) {
Log.i(TAG, "Registration not found.");
return "";
}
int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION,
Integer.MIN_VALUE);
int currentVersion = getAppVersion(context);
if (registeredVersion != currentVersion) {
Log.i(TAG, "App version changed.");
return "";
}
return registrationId;
}
private static int getAppVersion(Context context) {
try {
PackageInfo packageInfo = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionCode;
} catch (NameNotFoundException e) {
// should never happen
throw new RuntimeException("Could not get package name: " + e);
}
}
private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(activity);
}
regid = gcm.register(SENDER_ID);
msg = "Device registered, registration ID=" + regid;
storeRegistrationId(activity, regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
Toast.makeText(activity, msg, Toast.LENGTH_LONG).show();
}
}.execute(null, null, null);
}
private void storeRegistrationId(Context context, String regId) {
int appVersion = getAppVersion(context);
Editor editor = prefs.edit();
editor.putString(PROPERTY_REG_ID, regId);
editor.putInt(PROPERTY_APP_VERSION, appVersion);
editor.commit();
}}
類在我MainActivity
我試圖獲得註冊ID以下列方式
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
functions = new Functions(MainActivity.this);
functions.deviceRegistrationID();
registrationId = functions.getGCMRegistrationID();}
這裏registrationId
產生null
值。但在Functions
類方法
private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
.......
}
@Override
protected void onPostExecute(String msg) {
Toast.makeText(activity, msg, Toast.LENGTH_LONG).show();
}
}.execute(null, null, null);
}
它示出了在Toast
和註冊ID
public String getGCMRegistrationID(){
String registeredID = prefs.getString(PROPERTY_REG_ID, "");
System.out.println("The REG ID:"+prefs.getString(PROPERTY_REG_ID, ""));
return registeredID;
}
它示出了在LogCat
empty
註冊ID。 我如何獲得註冊ID?
檢查此有你在menifest <接收機得到這個接收器 android:name =「com.google.android.gcm.GCMBroadcastReceiver」 android:permission =「com.google.android.c2dm.permission.SEND」> < intent-filter> –
2014-11-25 05:11:21