【问题标题】:Google GCMIntentService instantiate [duplicate]Google GCMIntentService 实例化 [重复]
【发布时间】:2012-07-04 15:34:56
【问题描述】:

可能重复:
Android RuntimeException: Unable to instantiate the service

尝试启动并运行 GCM,但我的设备正在发送注册请求,但没有从 GCM 服务器得到响应。下面我包括了我的清单和 GCMIntentService 定义,以及我向 GCM 服务器注册的调用。我一直盯着它太久了,我可能会遗漏一些明显的东西,如果是这样,我深表歉意。

欢迎所有想法!

这是我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="PACKAGE"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <permission android:name="PACKAGE.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="PACKAGE.permission.C2D_MESSAGE" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <service android:name=".GCMIntentService" 
            android:enabled="true"/>

        <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.GCMIntentService" />
            </intent-filter>
        </receiver>


        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是我的 GCMIntentService 来处理来自 GCM 的返回:

import com.google.android.gcm.GCMBaseIntentService;
import android.content.Context;
import android.content.Intent;
import android.util.Log;


public class GCMIntentService extends GCMBaseIntentService {
    public static String TAG = "GCMIntentService";

    public GCMIntentService(String senderId) {
        super(senderId);
        Log.d("GCMIntentService", senderId);
    }

    @Override
    protected void onError(Context arg0, String arg1) {
        Log.d("onError", arg1);
    }

    @Override
    protected boolean onRecoverableError(Context context, String errorId){
        Log.d("onRecoverableError", errorId);
        return false;
    }

    @Override
    protected void onMessage(Context arg0, Intent arg1) {
        Log.d("onMessage", String.valueOf(arg1));
    }

    @Override
    protected void onRegistered(Context arg0, String arg1) {
        Log.d("onRegistered", arg1);
    }

    @Override
    protected void onUnregistered(Context arg0, String arg1) {
        Log.d("onUnregistered", arg1);
    }
}

我尝试在我的主类的 onCreate 方法中注册以下内容:

GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);

final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
    GCMRegistrar.register(this, SENDER_ID);
} else {
    Log.d(TAG, "Already registered");
}

更新 我已经尝试了 Raz 的建议,但出现以下错误:

07-03 12:07:40.459: W/dalvikvm(341): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-03 12:07:40.508: E/AndroidRuntime(341): FATAL EXCEPTION: main
07-03 12:07:40.508: E/AndroidRuntime(341): java.lang.RuntimeException: Unable to instantiate service package.gcm2.GCMIntentService: java.lang.InstantiationException: package.gcm2.GCMIntentService
07-03 12:07:40.508: E/AndroidRuntime(341):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:2943)
07-03 12:07:40.508: E/AndroidRuntime(341):  at android.app.ActivityThread.access$3300(ActivityThread.java:125)
07-03 12:07:40.508: E/AndroidRuntime(341):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2087)
07-03 12:07:40.508: E/AndroidRuntime(341):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 12:07:40.508: E/AndroidRuntime(341):  at android.os.Looper.loop(Looper.java:123)
07-03 12:07:40.508: E/AndroidRuntime(341):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-03 12:07:40.508: E/AndroidRuntime(341):  at java.lang.reflect.Method.invokeNative(Native Method)
07-03 12:07:40.508: E/AndroidRuntime(341):  at java.lang.reflect.Method.invoke(Method.java:521)
07-03 12:07:40.508: E/AndroidRuntime(341):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-03 12:07:40.508: E/AndroidRuntime(341):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-03 12:07:40.508: E/AndroidRuntime(341):  at dalvik.system.NativeStart.main(Native Method)
07-03 12:07:40.508: E/AndroidRuntime(341): Caused by: java.lang.InstantiationException: package.gcm2.GCMIntentService
07-03 12:07:40.508: E/AndroidRuntime(341):  at java.lang.Class.newInstanceImpl(Native Method)
07-03 12:07:40.508: E/AndroidRuntime(341):  at java.lang.Class.newInstance(Class.java:1429)
07-03 12:07:40.508: E/AndroidRuntime(341):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:2940)
07-03 12:07:40.508: E/AndroidRuntime(341):  ... 10 more
07-03 12:08:34.560: I/Process(341): Sending signal. PID: 341 SIG: 9

【问题讨论】:

    标签: android google-cloud-messaging


    【解决方案1】:

    在你的清单中试试这个:

    <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="eimmer.liav.elucidate.gcm2" />
            </intent-filter>
        </receiver>
    

    类别名称需要是您的应用程序包。

    如果服务在同一个包名中,请像您一样保留它,否则将其更改为您的真实包而不是.GCMIntentService,但您还需要extendoverride GCMBroadcastReceiver

    如果您的包不是 eimmer.liav.elucidate.gcm2.GCMIntentService,那么您必须扩展 GCMBroadcastReceiver 并覆盖函数 getGCMIntentServiceClassName()。 在该函数中,以字符串示例的形式返回真正的包和类:eimmer.liav.example.GCMIntentService

    另外不要忘记将接收者名称更改为新名称。示例:

     <receiver
            android:name="eimmer.liav.example.MyGCMBroadcastReceiver"
            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="eimmer.liav.elucidate.gcm2" />
            </intent-filter>
        </receiver>
    

    更多更改您的服务名称为:

    <service android:name="eimmer.liav.example.GCMIntentService" 
                android:enabled="true"/>
    

    根据 Google 的另一件事,您需要 将构造函数更改为默认构造函数,并发送如下字符串:

    public GCMIntentService() {
            super("Test");
        }
    

    据我所知,字符串并不重要。

    玩得开心。

    【讨论】:

    • 我确实尝试过,但后来我收到一个错误“无法实例化服务...”(我修改了我的原始帖子以包含完整的跟踪。
    • 你不打算扩展 GCMBaseIntentService 吗?
    • 带有杂项的默认构造函数。字符串是我似乎想念的那部分......谢谢!!!
    • 我实现了上面的解决方案,发现服务器不可用时出现问题(GCM错误=SERVICE_NOT_AVAILABLE)。我的 GCMIntentService::onError 从未收到错误通知,我认为这是因为错误:“W/ActivityManager(351): Unable to start service Intent { act=com.google.android.gcm.intent.RETRY flg= 0x14 cmp=my.package.name/.GCMIntentService(有附加功能)}:未找到”。 IE。尽管有上述解决方案,GCM 代码仍在应用程序包中寻找我的 GCMIntentService。 (尝试在清单中向我的自定义 GCM 接收器添加“com.google.android.gcm.intent.RETRY”操作。)
    • 官方教程让我相信GCMIntentService是实现GCMBroadcastReceiver。有了这个,我得到了错误。当我将其切换到 GCMBaseIntentService 时,它已修复。
    【解决方案2】:

    您的代码中的错误在于您的 GCMIntentService,您需要使用 NO 参数覆盖默认构造函数。要仍然传递 senderID,请执行以下操作:

    public static final String SENDER_ID = "sender-id-here";
    
    public GCMIntentService() 
    {
        super(SENDER_ID);
    }
    

    这很可能是人们的常见问题,因为 eclipse 会自动生成一个以字符串为参数的受保护构造函数。

    【讨论】:

    • 如果 com.google.android.gcm.GCMBroadcastReceiver 在实例化 GCMIntentService 时传递一个上下文而不是使用无参数构造函数,这样它就可以从资源中查找发件人 ID 而不是拥有将其硬编码到类中。
    • @user190758 您实际上不必对其进行硬编码。在您的构造函数中,只需调用无参数 super()。然后,您必须覆盖 GCMService 中的 getSenderIds(Context context),这样您就可以从资源中获取 senderId。
    【解决方案3】:

    这个意图服务将由 GCMBroadcastReceiver(它 是由 GCM 库提供的),如下图所示。一定是 com.google.android.gcm.GCMBaseIntentService 的子类,必须 包含一个公共构造函数,并且应该命名为 my_app_package.GCMIntentService (除非您使用 GCMBroadcastReceiver 覆盖用于命名 服务)。

    您应该重写 GCMBroadcastReceiver 类的 getGCMIntentServiceClassName 方法并设置您的自定义 IntentService 类名。然后修复 AndroidManifest.xml

    更多细节和示例代码你可以找到here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-04
      • 2021-02-11
      • 2017-01-17
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多