【问题标题】:onHandleIntent in GcmIntentService not getting calledGcmIntentService 中的 onHandleIntent 没有被调用
【发布时间】:2014-01-07 10:26:21
【问题描述】:

我正在关注来自 android 开发人员的 GCM 演示。得到以下情况。我在 GCMIntentService 中的 OnHandleIntent 没有被调用。有人可以帮忙吗?

package com.xyz.ads;


import com.google.android.gms.gcm.GoogleCloudMessaging;

import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class GcmIntentService extends IntentService {
    public GcmIntentService() {
        super("GcmIntentService");
        //super.onCreate();
        //super.onHandleIntent();
    }

    public void onCreate() {
        super.onCreate();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, startId, startId);
        Log.i("LocalService", "Received start id " + startId + ": " + intent);

        return START_STICKY;
    }
    public static final String TAG = "GCM Demo";

    @Override
    protected  void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }
}

这是我的广播接收器

package com.xyz.ads;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;


public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),GcmIntentService.class.getName());

        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

还有我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xyz.ads"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />
    <!-- For Google Cloud Services -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <!-- location -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <permission
        android:name="com.xyz.ads.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.xyz.ads.permission.C2D_MESSAGE" />
    <application
        android:name="ADS"
        android:allowBackup="true"
        android:debuggable="true"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/Theme.AppCompat"
        >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name=".ui.DummyActivity"
            android:label="@string/title_activity_dummy" >
        </activity>


        <!-- For Google Cloud Services -->
        <receiver
            android:name=".GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.xyz.ads" />
            </intent-filter>
        </receiver>

        <service
            android:name="GcmIntentService"
            android:enabled="true" >
        </service>

    </application>

</manifest>

onReceive 内部接收器被调用和意图服务的构造函数..

【问题讨论】:

  • 解决了吗...我也遇到同样的问题...请分享解决方案...

标签: android intentservice


【解决方案1】:

我会说你的班级名称中缺少一个点。

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

点代表您的应用程序的包名称。
假设你的包名是com.example.app。那么写.GcmIntentService和写com.example.app.GcmIntentService的效果是一样的。

【讨论】:

    【解决方案2】:

    试试这个

    <receiver
                android:name="com.xyz.ads.GcmBroadcastReceiver"
                android:permission="com.google.android.c2dm.permission.SEND" >
                <intent-filter>
    
                    <!-- Receives the actual messages. -->
                    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    
                    <category android:name="com.xyz.ads" />
                </intent-filter>
            </receiver>
    
     <service
                android:name="com.xyz.ads.GcmIntentService"
                android:enabled="true" >
            </service>
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,但不是代码(一切都像你的,就像文档说的:http://developer.android.com/google/gcm/client.html),而是 Eclipse 项目。

      如果对您有帮助,请勾选:

      进行重构并将服务从一个包移动到另一个包。检查 androidManifest.xml 是否不会自动更改包。如果不是,那你和我有同样的问题。

      我通过删除 ServiceIntent 类并创建一个新类(具有相同的内容)解决了这个问题。似乎我的服务已断开连接或类似的事情,并且当时我意识到通过比较代码我已经疯了。

      使用新的,如果再次检查,您将看到 androidmanifest.xml 中的包发生变化,并且 GcmBroadcastReceiver 调用了您的服务! :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多