我按照说明here
以下是代码:
if (checkPlayService()) {
gcm = GoogleCloudMessaging.getInstance(context);
regid = getRegistrationId(context);
if (regid.isEmpty()) {
registerInBackground();
} else {
Log.i(TAG, "No valid Google Play Services APK found.");
}
}
private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
String msg = "";
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
try {
Log.i(TAG, "Working in Background");
Log.i(TAG, "RegId = "+regid);
regid = gcm.register(SENDER_ID);
Log.i(TAG, "RegId = "+regid);
} catch (IOException e) {
// TODO Auto-generated catch block
msg = "Error :" + e.getMessage();
Log.i(TAG, msg);
}
msg = "Device Register with " + regid;
Log.i(TAG, msg);
sendRegistrationIdToBackend();
// For this demo: we don't need to send it because the device
// will send upstream messages to a server that echo back the
// message using the 'from' address in the message.
// Persist the regID - no need to register again.
storeRegistrationId(context, regid);
return msg;
}
@Override
protected void onPostExecute(String msg) {
registrationid = msg + "\n";
Log.i(TAG, "Registration ID"+registrationid);
}
}.execute(null, null, null);
}
protected void storeRegistrationId(Context context, String regid) {
// TODO Auto-generated method stub
Log.i(TAG, regid);
}
protected void sendRegistrationIdToBackend() {
// TODO Auto-generated method stub
final SharedPreferences prefs = getGCMPreferences(context);
int appVersion = getAppVersion(context);
Log.i(TAG, "Saving regId on app version " + appVersion);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(PROPERTY_REG_ID, regid);
editor.putInt(PROPERTY_APP_VERSION, appVersion);
editor.commit();
}
public String getRegistrationId(Context context) {
Log.i(TAG, "Inside getRegistrationId function");
final SharedPreferences prefs = getGCMPreferences(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 int getAppVersion(Context context) {
// TODO Auto-generated method stub
try {
PackageInfo packageInfo = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionCode;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
throw new RuntimeException("Could not get package name: " + e);
}
}
private SharedPreferences getGCMPreferences(Context context) {
// TODO Auto-generated method stub
return getSharedPreferences(UserRegister.class.getSimpleName(),
Context.MODE_PRIVATE);
}
public boolean checkPlayService() {
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
Log.i(TAG, "Check Play Services" + resultCode);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Log.i(TAG, "This device is not supported");
finish();
}
return false;
}
return true;
}
当我运行代码时,我得到以下Log cat值:
07-22 09:58:15.069: D/dalvikvm(32393): GC_CONCURRENT freed 127K, 3% free 6716K/6919K, paused 2ms+2ms
07-22 09:58:15.098: I/UserRegistration(32393): Check Play Services0
07-22 09:58:15.098: I/UserRegistration(32393): Inside getRegistrationId function
07-22 09:58:15.108: I/UserRegistration(32393): Registration not found.
07-22 09:58:15.108: I/UserRegistration(32393): Working in Background
07-22 09:58:15.108: I/UserRegistration(32393): RegId =
07-22 09:58:15.108: I/UserRegistration(32393): Check Play Services0
07-22 09:58:15.108: I/UserRegistration(32393): Inside getRegistrationId function
07-22 09:58:15.108: I/UserRegistration(32393): Registration not found.
07-22 09:58:15.118: I/UserRegistration(32393): Working in Background
07-22 09:58:15.118: I/UserRegistration(32393): RegId =
如果我使用下面的代码段,当我将AsychTask
放在log.i
regid = gcm.register(SENDER_ID);
上方时, Log.i(TAG, "Working in Background");
regid = gcm.register(SENDER_ID);
Log.i(TAG, "RegId = "+regid);
我遇到的问题我正在使用以下代码段:
07-22 10:10:55.748: W/dalvikvm(432): threadid=11: thread exiting with uncaught exception (group=0x2b542210)
07-22 10:10:55.748: W/dalvikvm(432):
threadid=12: thread exiting with uncaught exception (group=0x2b542210)
Log cat:
regid
请帮助我,我无法找出为什么会发生这种情况?我也无法生成07-22 11:16:35.868: I/UserRegistration(2040): Check Play Services0
07-22 11:16:35.868: I/UserRegistration(2040): Inside getRegistrationId function
07-22 11:16:35.868: I/UserRegistration(2040): Registration not found.
07-22 11:16:35.868: I/UserRegistration(2040): Working in Background
07-22 11:16:35.878: I/UserRegistration(2040): Check Play Services0
07-22 11:16:35.878: I/UserRegistration(2040): Inside getRegistrationId function
07-22 11:16:35.878: I/UserRegistration(2040): Registration not found.
07-22 11:16:35.878: I/UserRegistration(2040): Error :null
07-22 11:16:35.878: I/UserRegistration(2040): Device Register with
07-22 11:16:35.928: I/UserRegistration(2040): Working in Background
07-22 11:16:35.948: I/UserRegistration(2040): Error :null
07-22 11:16:35.948: I/UserRegistration(2040): Device Register with
。
我已经修改了Catch而不是IOException我将其更改为Exception然后我得到以下日志
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.splitwise"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission
android:name="android.permission.INTERNET"></uses-permission>
<uses-permission
android:name="android.permission.GET_ACCOUNTS"></uses-permission>
<uses-permission
android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission
android:name="com.google.android.c2dm.permission.RECEIVE"></uses-permission>
<uses-permission
android:name="com.example.registration.permission.C2D_MESSAGE"
android:protectionLevel="signature"></uses-permission>
<uses-permission android:name="com.example.registration.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.registration.UserRegister"
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="com.facebook.LoginActivity"></activity>
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"></meta-data>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<receiver
android:name="com.example.registration.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.registration" />
</intent-filter>
</receiver>
<service android:name="com.example.registration.GcmIntentService" />
</application>
现在它将转到其他行,但我无法获得它显示为null的RegId。
{{1}}
答案 0 :(得分:0)
此行抛出异常:
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
每当您尝试运行不支持 Google Play服务的应用设备时,它都会抛出Exception
,
不支持Google Play服务的设备: https://support.google.com/googleplay/answer/1727131?hl=en-IN https://developer.android.com/google/play-services/index.html
所以你应该使用try catch来实现该方法。
最后的结论是您的设备不支持Google Play Services
或已禁用(需要安装)它。
看看这个(Kindle设备上的同样问题): GCM is not working on Kindle fire devices: java.lang.UnsupportedOperationException: Device does not have package com.google.android.gsf