產生REGID我跟着指示here未捕獲的異常而在GCM
下面是代碼:
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;
}
當我跑我得到下面的日誌貓值碼:
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);
以上時我得到的是日誌消息if我使用下面的代碼片段:
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)
請幫助我,我無法找出爲什麼發生這種情況?我也無法生成regid
。
我已經修改了釣位,而不是IOException異常我必須將其更改爲異常,然後我得到了下面的日誌
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
現在它要其他線路,但我不能得到的RegID它顯示爲空。
<?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>
感謝您的幫助,我已經記錄此行Log.i(TAG,「檢查播放服務」 + resultCode爲)後的結果代碼;如果您看到日誌貓消息正在顯示Check Play Services0,則表示結果代碼爲0,即Success。如果設備不受支持,它會將消息記錄爲「此設備不受支持」,因爲我在finish()之前記錄了它。 –
然後這將解決您的問題?如果發生異常,則從catch塊返回。 –
如果在Async任務中發生異常,那麼它將顯示一些消息,但它說的是未捕獲的異常。 –