gpt4 book ai didi

GCMIntentServce onRegistered 成功后 Android 应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 02:23:49 24 4
gpt4 key购买 nike

我遇到了这样的情况:全新安装的 Android 应用程序在到达 GCMItentService 文件中的 onRegistered 函数后会崩溃。 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));
}

这是我的 GCMItentService 文件

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

这是 list 文件

<?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>

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

最佳答案

您是否在 list 上注册了接收者和 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>

关于GCMIntentServce onRegistered 成功后 Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16218398/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com