【问题标题】:App crashes with java.lang.IllegalStateException: Not allowed to start service Intent while receiving notification in background应用程序因 java.lang.IllegalStateException 崩溃:在后台接收通知时不允许启动服务 Intent
【发布时间】:2018-06-19 12:16:00
【问题描述】:

Xamarin 构建针对 api 级别 26 的 Android 应用程序崩溃并出现 java.lang.IllegalStateException: Not allowed to start service Intent 在后台收到通知时。

来自Notification in oreo,我的理解是在应用白名单期间只有应用可以提供服务。如何正确处理这种情况,我在收到通知时调用并更新我的数据库,我该如何正确管理。

一个答案显示

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
        context.startForegroundService(new Intent(context, ServedService.class));
    } else {
        context.startService(new Intent(context, ServedService.class));
    }

但是我如何在 xamarin 表单应用程序中实现这一点以正确接收推送通知。

【问题讨论】:

  • 您想在 Xamarin 表单中实现代码吗?如果是这样,你想在哪里调用它?聚氯乙烯? Android 平台?

标签: android xamarin push-notification xamarin.forms android-8.0-oreo


【解决方案1】:

从 Android 8.0(API 级别 26)开始。

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

阅读Android 8.0 Behavior Changes 了解如何在 android Oreo 中工作。您可以在收到通知时使用Job scheduler 来执行数据库任务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-08
    • 2020-05-19
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多