android– 从库项目注册GCM

android– 从库项目注册GCM,第1张

概述我正在尝试从库项目注册到GCM,GCMIntentService是在库项目中定义的.这是我的注册码(在图书馆项目内):publicstaticvoidinit(Contextcontext,Stringid){GCMRegistrar.checkDevice(context);GCMRegistrar.checkManifest(context);GCMRegistrar.register(conte

我正在尝试从库项目注册到GCM,GCMIntentService是在库项目中定义的.

这是我的注册码(在图书馆项目内):

public static voID init(Context context, String ID){    GCMRegistrar.checkDevice(context);    GCMRegistrar.checkManifest(context);    GCMRegistrar.register(context, ID);}

但是没有调用GCMIntentService的回调,只有当我将库项目作为独立的androID项目运行时才调用它们.

这是我的测试项目清单:

<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"    package="com.example.pushtest"    androID:versionCode="1"    androID:versionname="1.0" >    <uses-sdk        androID:minSdkVersion="8"        androID:targetSdkVersion="17" />    <permission        androID:name="com.example.pushtest.permission.C2D_MESSAGE"        androID:protectionLevel="signature" />    <uses-permission androID:name="androID.permission.INTERNET" />    <uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE" />    <uses-permission androID:name="androID.permission.VIBRATE" />    <!-- GCM requires a Google account. -->    <uses-permission androID:name="androID.permission.GET_ACCOUNTS" />    <uses-permission androID:name="androID.permission.WAKE_LOCK" />    <uses-permission androID:name="com.Google.androID.c2dm.permission.RECEIVE" />    <uses-permission androID:name="com.example.pushtest.permission.C2D_MESSAGE" />    <application        androID:allowBackup="true"        androID:icon="@drawable/ic_launcher"        androID:label="@string/app_name"        androID:theme="@style/Apptheme"        androID:name=".PushTestApp" >        <activity            androID:name="com.example.pushtest.MainActivity"            androID:label="@string/app_name" >            <intent-filter>                <action androID:name="androID.intent.action.MAIN" />                <category androID:name="androID.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <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.example.pushtest" />            </intent-filter>        </receiver>    </application>    //push library deFinition    <service androID:name="com.test.pushlibrary.GCMIntentService" /></manifest>

解决方法:

这是你要做的:

编写一个覆盖GCMbroadcastReceiver的类(在您的库项目中):

package com.test.pushlibrary;import androID.content.Context;import com.Google.androID.gcm.GCMbroadcastReceiver;public class PushlibrarybroadcastReceiver extends GCMbroadcastReceiver{    /**     * Gets the class name of the intent service that will handle GCM messages.     */    @OverrIDe    protected String getGCMIntentServiceClassname(Context context) {        return "com.test.pushlibrary.GCMIntentService";    }}

更改清单以引用新接收器:

    <receiver        androID:name="com.test.pushlibrary.PushlibrarybroadcastReceiver"        ...

原始代码不起作用的原因是getGCMIntentServiceClassname的默认实现假定intent服务类位于应用程序的包中,因此它需要com.example.pushtest.GCMIntentService而不是com.test.pushlibrary. GCMIntentService.

总结

以上是内存溢出为你收集整理的android – 从库项目注册GCM全部内容,希望文章能够帮你解决android – 从库项目注册GCM所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1102517.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存