android - GcmIntentService 中的 onHandleIntent 未被调用

标签 android intentservice

我正在关注来自 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);
    }
}

这是我的 BroadCastReceiver

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);
    }
}

和我的 list

<?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 和 Intent 服务的构造函数..

最佳答案

我会说你在类(class)名称中缺少一个点。

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

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

关于android - GcmIntentService 中的 onHandleIntent 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20969212/

相关文章:

android - 使用机器人框架脚本和 chromedriver 在 Android 设备中打开 Chrome 浏览器?

android - jQuery UI Touch Punch 不适用于 Android 设备上的 Chrome 和 Opera

android - socket.io Android 库认证

Android进度条和服务

android - 显示空白屏幕 Android 的 Google map

android - 自定义样式的 RatingBar 不显示

android - WakefulBroadcastReceiver 中 IntentService 中的 BroadcastReceiver 并不总是有效

java - Android通知IntentService中的deleteIntent调用函数

Android IntentService/Service 向 web 服务请求/发送数据并获取 UI 进度

android - 在 onHandleIntent 之前捕获 IntentService Intent