注册GCM从库项目 [英] Register GCM from a library project
问题描述
我想从一个库项目的GCMIntentService是库项目中定义注册到GCM。
下面是我的注册code(库项目中):
公共静态无效的init(上下文的背景下,字符串ID){
GCMRegistrar.checkDevice(上下文);
GCMRegistrar.checkManifest(上下文);
GCMRegistrar.register(上下文,ID);
}
但GCMIntentService都没有被称为回调,它们被称为只有当我运行库项目作为独立的Android项目。
下面是我的测试项目清单:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.pushtest
安卓版code =1
机器人:VERSIONNAME =1.0>
<用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =17/>
<许可
机器人:名称=com.example.pushtest.permission.C2D_MESSAGE
安卓的ProtectionLevel =签名/>
<使用-权限的Android:名称=android.permission.INTERNET对/>
<使用-权限的Android:名称=android.permission.ACCESS_NETWORK_STATE/>
<使用-权限的Android:名称=android.permission.VIBRATE/>
<! - GCM需要一个谷歌帐户。 - >
<使用-权限的Android:名称=android.permission.GET_ACCOUNTS/>
<使用-权限的Android:名称=android.permission.WAKE_LOCK/>
<使用-权限的Android:名称=com.google.android.c2dm.permission.RECEIVE/>
<使用-权限的Android:名称=com.example.pushtest.permission.C2D_MESSAGE/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme
机器人:名称=PushTestApp。>
<活动
机器人:名称=com.example.pushtest.MainActivity
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<接收器
机器人:名称=com.google.android.gcm.GCMBroadcastReceiver
机器人:权限=com.google.android.c2dm.permission.SEND>
<意向滤光器>
<作用机器人:名称=com.google.android.c2dm.intent.RECEIVE/>
<作用机器人:名称=com.google.android.c2dm.intent.REGISTRATION/>
<类机器人:名称=com.example.pushtest/>
&所述; /意图滤光器>
< /接收器>
< /用途>
//推库定义
<服务机器人:名称=com.test.pushlibrary.GCMIntentService/>
< /舱单>
下面是你必须做的:
写一个类(在库项目),其覆盖GCMBroadcastReceiver:
包com.test.pushlibrary;
进口android.content.Context;
进口com.google.android.gcm.GCMBroadcastReceiver;
公共类PushLibraryBroadcastReceiver扩展GCMBroadcastReceiver
{
/ **
*获取意图服务,将处理GCM消息的类名。
* /
@覆盖
保护字符串getGCMIntentServiceClassName(上下文的背景下){
返回com.test.pushlibrary.GCMIntentService;
}
}
更改您的清单以引用新的接收器:
<接收器
机器人:名称=com.test.pushlibrary.PushLibraryBroadcastReceiver
...
这是你原来的code没有工作的原因是, getGCMIntentServiceClassName
的默认实现假定意图服务类位于应用程序的软件包,因此该公司预计, com.example.pushtest.GCMIntentService
而不是 com.test.pushlibrary.GCMIntentService
。
I am trying to register to GCM from a library project the GCMIntentService is defined inside the library project.
Here is my registration code(inside the library project):
public static void init(Context context, String id){
GCMRegistrar.checkDevice(context);
GCMRegistrar.checkManifest(context);
GCMRegistrar.register(context, id);
}
But the callbacks of the GCMIntentService are not being called, they are being called only when I run the library project as standalone android project.
Here is my test project manifest:
<?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>
Here's what you have to do :
Write a class (in your library project) that overrides 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";
}
}
Change your manifest to refer to the new receiver :
<receiver
android:name="com.test.pushlibrary.PushLibraryBroadcastReceiver"
...
The reason that your original code didn't work is that the default implementation of getGCMIntentServiceClassName
assumes that the intent service class is located in the package of the application, so it's expecting com.example.pushtest.GCMIntentService
instead of com.test.pushlibrary.GCMIntentService
.
这篇关于注册GCM从库项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!