我已经在我的应用程序中实现了 GCM,但遇到了一个奇怪的问题。
当我的 GCMBaseIntentService
的重写 IntentService
位于我的根包中时,它工作正常。我在 list 中使用 .MyGCMintentService
引用它。
根包将是 com.example.rootpackage
当我将 Intent 服务移动到不同的包时,例如com.example.rootpackage.service
, Intent 服务永远不会被调用。此时,我将更新我的 list 以指向 com.example.rootpackage.service.MyGCMItentService
并且没有骰子。
我是否遗漏了 Google 文档中有关查找它的内容,或者这就是它的工作原理?
最佳答案
是的,它应该在根包中:
This intent service will be called by the GCMBroadcastReceiver (which is provided by the GCM library), as shown in the next step. It must be a subclass of com.google.android.gcm.GCMBaseIntentService, must contain a public constructor, and should be named my_app_package.GCMIntentService (unless you use a subclass of GCMBroadcastReceiver that overrides the method used to name the service).
(引自 here )
编辑:
正如文档所述,如果您使用覆盖 getDefaultIntentServiceClassName
的 GCMBroadcastReceiver
子类,则可以更改它:
public class GCMBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "GCMBroadcastReceiver";
private static boolean mReceiverSet = false;
@Override
public final void onReceive(Context context, Intent intent) {
Log.v(TAG, "onReceive: " + intent.getAction());
// do a one-time check if app is using a custom GCMBroadcastReceiver
if (!mReceiverSet) {
mReceiverSet = true;
String myClass = getClass().getName();
if (!myClass.equals(GCMBroadcastReceiver.class.getName())) {
GCMRegistrar.setRetryReceiverClassName(myClass);
}
}
String className = getGCMIntentServiceClassName(context);
Log.v(TAG, "GCM IntentService class: " + className);
// Delegates to the application-specific intent service.
GCMBaseIntentService.runIntentInService(context, intent, className);
setResult(Activity.RESULT_OK, null /* data */, null /* extra */);
}
/**
* Gets the class name of the intent service that will handle GCM messages.
*/
protected String getGCMIntentServiceClassName(Context context) {
return getDefaultIntentServiceClassName(context);
}
/**
* Gets the default class name of the intent service that will handle GCM
* messages.
*/
static final String getDefaultIntentServiceClassName(Context context) {
String className = context.getPackageName() +
DEFAULT_INTENT_SERVICE_CLASS_NAME;
return className;
}
}
关于android - GCMBaseIntentService 回调仅在根包中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16951216/