如何停止或取消注册GCMIntentService服务?

时间:2013-03-01 13:34:43

标签: android service push google-cloud-messaging

我正在使用GCMIntentService接收GCM消息。这一切都按预期工作,但我允许用户退出"退出"或者停止应用程序,之后不应该向设备发送新消息。

问题是,如何阻止GCMIntentService运行?因为在我关闭所有东西并打电话后#34;完成"在我的Activity上,GCMIntentService仍然会收到一些导致NullRef的消息。

当然,我可以使用if语句来避免nullRef,但是我想完全停止GCMIntentService,以便它不再运行。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

忽略你从GCM得到的消息怎么样? 假设您有一个布尔值确定GCM状态,关闭时您只是忽略传入的消息。

// 见http://developer.android.com/reference/com/google/android/gcm/GCMRegistrar.html#unregister(android.content.Context) public static void unregister (Context context)

中的GCMRegistrar

答案 1 :(得分:0)

如果有人在寻找答案,我的解决方案是:
在onDestroy中禁用我的GCMListener类Intent-Filter:

getPackageManager().setComponentEnabledSetting(new ComponentName(getApplicationContext(), MyGCMListenerClass.class), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);

在OnResume中重新启用:

getPackageManager().setComponentEnabledSetting(new ComponentName(getApplicationContext(), GCMListener.class), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

P.S。 所有GCM消息都将被忽略,因此您以后不会收到它们。