【问题标题】:Android 26 Unable to start receiver com.google.android.gcm.GCMBroadcastReceiver: java.lang.IllegalStateException: Not allowed to start service Intent [duplicate]Android 26无法启动接收器com.google.android.gcm.GCMBroadcastReceiver:java.lang.IllegalStateException:不允许启动服务意图[重复]
【发布时间】:2018-05-24 20:32:52
【问题描述】:

我在 Android Oreo 中向我的游戏发送推送消息时收到错误消息。
该游戏适用于所有以前版本的 Android Oreo。
它只发生在 Android Oreo 中。
我在这个论坛上搜索了很多答案。
有人说我必须使用 FCM 而不是 GCM。
但我无法将旧的 Eclipse 项目更改为 Android Studio 因为有很多风险。
我使用 gcm.jar 库。
请帮助我并提前致谢!

Logcat

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start receiver com.google.android.gcm.GCMBroadcastReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010 pkg=com.example.test cmp=com.example.test/.GCMIntentService (has extras) }: app is in background uid UidRecord{7cd2325 u0a379 RCVR idle change:uncached procs:1 seq(0,0,0)} at android.app.ActivityThread.handleReceiver(ActivityThread.java:3194) at android.app.ActivityThread.-wrap17(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010 pkg=com.example.test cmp=com.example.test/.GCMIntentService (has extras) }: app is in background uid UidRecord{7cd2325 u0a379 RCVR idle change:uncached procs:1 seq(0,0,0)} at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1518) at android.app.ContextImpl.startService(ContextImpl.java:1474) at android.content.ContextWrapper.startService(ContextWrapper.java:649) at android.content.ContextWrapper.startService(ContextWrapper.java:649) at com.google.android.gcm.GCMBaseIntentService.runIntentInService(GCMBaseIntentService.java:282) at com.google.android.gcm.GCMBroadcastReceiver.onReceive(GCMBroadcastReceiver.java:55) at android.app.ActivityThread.handleReceiver(ActivityThread.java:3187)

【问题讨论】:

  • 我在这个论坛上搜索了很多答案。 - 你搜索的不是很好。我刚刚复制了您的错误并发现了完全相同的问题
  • 很多人告诉我参考“后台执行限制”的手册。或者他们说我必须使用“startForegroundService”而不是“startService”。但我不明白该怎么做。我只是在 AndroidManifest.xml 文件中使用 ''。我使用'public class GCMIntentService extends GCMBaseIntentService { ... }'。就这样。它适用于除 android oreo 之外的所有版本的 android。请给我一个示例代码。
  • @KiSeokKim 你有例子吗?

标签: android eclipse google-cloud-messaging


【解决方案1】:

从 Android O 开始。

如果面向 Android 8.0 的应用尝试在不允许创建后台服务的情况下使用该方法,则 startService() 方法现在会引发 IllegalStateException。

默认情况下,这些限制仅适用于以 O 为目标的应用,但如果用户为特定应用启用这些限制,则应用将被视为相同。 有关主题的更多信息,请阅读后台执行限制部分here

【讨论】:

  • 很多人告诉我参考“后台执行限制”的手册。或者他们说我必须使用“startForegroundService”而不是“startService”。但我不明白该怎么做。我只是在 AndroidManifest.xml 文件中使用 ''。我使用'public class GCMIntentService extends GCMBaseIntentService { ... }'。就这样。它适用于除 android oreo 之外的所有版本的 android。请给我一个示例代码。
  • @KiSeokKim 你有解决办法吗?
【解决方案2】:

从 API 26 开始,您将无法在后台启动或销毁服务。

https://developer.android.com/about/versions/oreo/background.html

更多详情请阅读此处。

由于电池优化和一些安全原因,API 26 有很多限制。

【讨论】:

  • 很多人告诉我参考“后台执行限制”的手册。或者他们说我必须使用“startForegroundService”而不是“startService”。但我不明白该怎么做。我只是在 AndroidManifest.xml 文件中使用 ''。我使用'public class GCMIntentService extends GCMBaseIntentService { ... }'。就这样。它适用于除 android oreo 之外的所有版本的 android。请给我一个示例代码。
猜你喜欢
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-03
  • 2018-12-23
  • 2011-03-27
相关资源
最近更新 更多