0

我遇到的情况是,全新安装的 Android 应用程序在到达onRegisteredGCMIntentService文件中的函数后会崩溃。GCM 注册成功,我可以在下次打开应用程序时检索 GCM 注册 ID,但我不希望它在用户第一次打开它时崩溃。

我用它来要求注册:

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>

任何关于为什么会发生这种情况的想法将不胜感激。

4

2 回答 2

0

您是否在 Manifest 上注册了 Receiver and Intent 服务?

例子:

<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="PACKAGE_NAME" />
                </intent-filter>
            </receiver>
            <service android:name=".GCMIntentService"></service>
于 2013-04-25T15:13:43.730 回答
0

您可以从以下路径获得正确的 jar:

android-sdk-windows\extras\google\gcm\gcm-client\dist

看看这个例子:

GCM 示例

于 2013-08-29T06:38:33.323 回答