gpt4 book ai didi

android - GCMBaseIntentService 回调仅在根包中

转载 作者:搜寻专家 更新时间:2023-11-01 08:58:48 25 4
gpt4 key购买 nike

我在我的应用程序中实现了 GCM,但遇到了一个奇怪的问题。当我为 GCMBaseIntentService 覆盖的 IntentService 位于我的根包中时,它工作正常。我在 list 中使用 .MyGCMIntentService 引用它。根包将是 com.example.rootpackage

当我将我的 Intent 服务移动到不同的包时,例如 com.example.rootpackage.service Intent 服务永远不会被调用。此时我会更新我的 list 以指向 com.example.rootpackage.service.MyGCMIntentService 并且没有骰子。

我是否遗漏了 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 )

编辑:

如文档所述,如果您使用覆盖 getDefaultIntentServiceClassNameGCMBroadcastReceiver 的子类,则可以更改它:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com