GCMIntentService中的onRegistered()未被调用

7

我正尝试在GCM上注册我的应用程序,但不知道为什么我的应用程序从未被注册。调用GCMRegistrar.register(this, SENDER_ID);,但我的GCMIntentService中的onRegistered()从未被调用。我不知道为什么

这是我的Logcat

01-17 11:03:00.015: D/GCMRegistrar(3509): resetting backoff for com.abc.xyz.ui.activity
01-17 11:03:03.210: V/GCMRegistrar(3509): Registering app com.abc.xyz.ui.activity of senders 964256581311
01-17 11:03:06.070: V/GCMBroadcastReceiver(3509): onReceive: com.google.android.c2dm.intent.REGISTRATION
01-17 11:03:06.070: V/GCMBroadcastReceiver(3509): GCM IntentService class: com.abc.xyz.ui.activity.GCMIntentService
01-17 11:03:06.070: V/GCMBaseIntentService(3509): Acquiring wakelock

这是我的完整清单。
    <?xml version="1.0" encoding="utf-8"?>
<manifest package="com.abc.xyz.ui.activity"
    android:versionCode="1"
    android:versionName="1.5.6" xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-sdk 
        android:minSdkVersion="11" android:targetSdkVersion="16"/>
    <uses-feature 
        android:name="android.hardware.usb.host"/>

    <!-- This app has permission to register and receive data message. -->
    <uses-permission
        android:name="com.google.android.c2dm.permission.RECEIVE" />

    <uses-permission 
        android:name="android.permission.INTERNET" />
    <uses-permission 
        android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission 
        android:name="android.permission.GET_TASKS" />
    <uses-permission 
        android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission
        android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission 
        android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission 
        android:name="android.permission.CALL_PHONE" /> 
    <uses-permission 
        android:name="android.permission.BLUETOOTH" />

    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 

    <!--
     Creates a custom permission so only this app can receive its messages.

     NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,
           where PACKAGE is the application's package name.
    -->
    <uses-permission
        android:name="com.abc.xyz.ui.activity.permission.C2D_MESSAGE" />   
    <permission
        android:name="com.abc.xyz.ui.activity.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />    
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.Holo.Light" 
        android:name="com.abc.xyz.MyApplication" 
        android:allowBackup="false">
        <activity
            android:name=".StartupActivity"
            android:noHistory="true"
            android:label="@string/title_startup_screen" 
            android:configChanges="orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LoginActivity"
            android:windowSoftInputMode="stateAlwaysVisible|adjustResize"
            android:label="@string/title_login_screen"
            android:configChanges="orientation">
        </activity>
        //my other activity defination

        <!--
          BroadcastReceiver that will receive intents from GCM
          services and handle them to the custom IntentService.

          The com.google.android.c2dm.permission.SEND permission is necessary
          so only GCM services can send data messages for the app.
        -->
        <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <!-- Receives the actual messages. -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <!-- Receives the registration id. -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.abc.xyz.ui.activity" />
            </intent-filter>
        </receiver>

        <!--
          Application-specific subclass of GCMBaseIntentService that will
          handle received messages.

          By default, it must be named .GCMIntentService, unless the
          application uses a custom BroadcastReceiver that redefines its name.
        -->
        <service android:name="com.abc.xyz.ui.activity.GCMIntentService" />

    </application>
</manifest>

我不知道出了什么问题,为什么onRegistered()从未被调用。任何帮助将不胜感激。


你如何验证它没有被调用?哦,你在谈论哪个方法?标题说onRegistered,正文说onReceived... - WarrenFaith
抱歉,我会进行更改。OnRegistered()是方法名。我已经放置了日志和调试点,但没有显示任何信息。 - Android
2个回答

10

在另一篇文章中,我找到了问题所在。我的GCMIntentService类在清单文件中被定义为

<service android:name="MY_PACKAGE.gcm.GCMIntentService" />
因为我不想把这个类放在根包中,所以我把它放在了 MY_PACKAGE.gcm 包中。这似乎引起了问题,正如文档所述,
请添加以下意图服务
<service android:name=".GCMIntentService" />

所以当我将它移动到根包时,它就可以工作了!还有另一种方法可以将GCMIntentServiceSubclass放在任何你想要的地方并命名不同。你应该继承GCMBroadcastReceiver并像这个帖子中所示,在清单和子类中进行更改。


这是我的问题。经过3个小时的调试,最终发现问题出在AndroidManifest文件上! - WOUNDEDStevenJones
谢谢!这对我很有帮助 :) - nithinreddy
哇!……这个可行。很烦人的是我不能把“gcmIntent”放在“my_package”里 :( - Pablo Pantaleon

2

我来回答这个问题,因为在评论中粘贴代码几乎无法阅读:

我看不到您的logcat中的一些日志,这些日志表明您的服务是否被调用。当我注册时,我的Logcat中有以下内容:

GCMBroadcastReceiver  V  onReceive: com.google.android.c2dm.intent.REGISTRATION
GCMBroadcastReceiver  V  GCM IntentService class: com.package.android.app.GCMIntentService
GCMBaseIntentService  V  Acquiring wakelock
GCMBaseIntentService  V  Intent service name: GCMIntentService-DynamicSenderIds-1

特别是最后一行缺失了或者你忘记粘贴了它。请确保你发布了完整的LogCat?筛选GCM以确保你没有漏掉任何东西。
更新
正如OP在评论中提到的,他使用了该服务来做更多的事情。这与GCM功能有些干扰,在将其分成两个服务后它就可以工作了。教训:不要将GCMIntentService用于除GCM之外的任何其他用途。

是的。与其他活动相同的包和在清单标记中定义的一个,即com.abc.xyz.ui.activity。 - Android
2
我很好奇为什么你在清单文件中使用了com.abc.xyz.ui.activity包,但是在应用程序包中却使用了com.abc.xyz。我不确定这是否会造成任何问题。GCMIntentService需要放置在非常特定的位置,通常您会使用根包,但由于您的清单包是应用程序包的子包...嗯,这很不寻常。 - WarrenFaith
LogCat 中是否有任何提示表明服务未能成功启动?我有点迷失在这里,因为我找不到任何问题...你是否检查过服务的其他方法是否被调用了? - WarrenFaith
不知道出了什么问题。我已经尝试了两个星期,但是还是得到相同的输出结果。 - Android
你说的“使用服务来做更多”的意思是什么?我只有回调函数和构造函数,我的onRegistered也从未被调用过。 - Raphael Oliveira
显示剩余14条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接