我遇到的情況是一個新的安裝我的Android應用程序的在我的GCMIntentService
文件達到onRegistered
功能後會崩潰。 GCM註冊成功,我可以在下次打開應用程序時檢索GCM註冊ID,但我不希望用戶第一次打開它時會崩潰。Android應用程序崩潰後GCMIntentServce onRegistered成功
我用它來打電話去報名:
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, getResources().getString(R.string.gcmToken));
}
這是我GCMIntentService
文件
public class GCMIntentService extends GCMBaseIntentService {
private static final String LOG_TAG = "GCM";
public GCMIntentService() {
super("###############"); // use my GCM ID
// TODO Auto-generated constructor stub
Log.i(LOG_TAG, "GCMIntentService constructor called");
}
@Override
protected void onError(Context arg0, String errorId) {
// TODO Auto-generated method stub
Log.i(LOG_TAG, "GCMIntentService onError called: " + errorId);
}
@Override
protected void onMessage(Context arg0, Intent intent) {
// TODO Auto-generated method stub
Log.i(LOG_TAG, "GCMIntentService onMessage called");
Log.i(LOG_TAG, "Message is: " + intent.getStringExtra("message"));
}
@Override
protected void onRegistered(Context arg0, String registrationId) {
// TODO Auto-generated method stub
Log.i(LOG_TAG, "GCMIntentService onRegistered called");
Log.i(LOG_TAG, "Registration id is: " + registrationId);
// ! ! ! CRASHES HERE ! ! !
}
@Override
protected void onUnregistered(Context arg0, String registrationId) {
// TODO Auto-generated method stub
Log.i(LOG_TAG, "GCMIntentService onUnregistered called");
Log.i(LOG_TAG, "Registration id is: " + registrationId);
}
}
這裏的logcat的
V/GCMBaseIntentService(9604): Releasing wakelock
W/dalvikvm(9604): threadid=10: thread exiting with uncaught exception (group=0x40020950)
E/AndroidRuntime(9604): FATAL EXCEPTION: IntentService[GCMIntentService-##########-1]
E/AndroidRuntime(9604): java.lang.ClassCastException: android.app.Application
E/AndroidRuntime(9604): at com.myfile.app.GCMIntentService.onRegistered(GCMIntentService.java:85)
E/AndroidRuntime(9604): at com.google.android.gcm.GCMBaseIntentService.handleRegistration(GCMBaseIntentService.java:296)
E/AndroidRuntime(9604): at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:197)
E/AndroidRuntime(9604): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
E/AndroidRuntime(9604): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(9604): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(9604): at android.os.HandlerThread.run(HandlerThread.java:60)
D/dalvikvm(9604): GC_FOR_MALLOC freed 3478 objects/299784 bytes in 815ms
這裏墜毀部分清單文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myfile.app"
android:versionCode="1"
android:versionName="1.0" >
<!-- B1 - BSG DEV -->
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE"/>
<!-- Google Cloud Messaging -->
<permission android:name="com.myfile.app.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.myfile.app.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Google Cloud Messaging -->
<application
android:allowBackup="true"
android:icon="@drawable/icon1"
android:label="@string/app_name" >
<activity
android:name="com.myfile.app.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.myfile.app.myclass" android:screenOrientation="portrait"/>
<!-- Google Cloud Messaging -->
<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.myfile.app" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
<!-- Google Cloud Messaging -->
</application>
</manifest>
爲什麼發生這種情況,將不勝感激任何想法。
你有一個'ClassCastException'。你想在'onRegistered()'的第85行施放什麼東西? – Karakuri 2013-04-25 15:09:25
這讓我感到困惑......我的GCMIntentService文件甚至不超過85行。 – Chris 2013-04-25 15:22:17