LocalNotification 与 AlarmManager 和 BroadcastReceiver 在 Android O (oreo) 中未启动

2024-03-12

我的本地通知在 SDK 26 之前的 Android 上运行

但在 Android O 中,我收到以下警告,并且广播接收器不会被触发。

W/BroadcastQueue: Background execution not allowed: receiving Intent { act=package.name.action.LOCAL_NOTIFICATION cat=[com.category.LocalNotification] flg=0x14 (has extras) } to package.name/com.category.localnotifications.LocalNotificationReceiver

据我所知,广播接收器在 android O 中受到更多限制,但如果是这样,如果我希望广播启动,即使主要活动没有运行,我应该如何安排广播?

我应该使用服务而不是接收器吗?

这是 AlarmManager 启动代码:

public void Schedule(String aID, String aTitle, String aBody, int aNotificationCode, long aEpochTime)
{
    Bundle lExtras = new Bundle();
    lExtras.putInt("icon", f.getDefaultIcon());
    lExtras.putString("title", aTitle);
    lExtras.putString("message", aBody);
    lExtras.putString("id", aID);
    lExtras.putInt("requestcode", aNotificationCode);

    Intent lIntent = 
      new Intent(LocalNotificationScheduler.ACTION_NAME)
      .addCategory(NotificationsUtils.LocalNotifCategory)
      .putExtras(lExtras);

    PendingIntent lPendIntent = PendingIntent.getBroadcast(f.getApplicationContext(), aNotificationCode,
                                                           lIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager lAlarmMgr = (AlarmManager) f.getSystemService(Context.ALARM_SERVICE);
    lAlarmMgr.set(AlarmManager.RTC, 1000, lPendIntent);
}

这是接收者代码:

public class LocalNotificationReceiver extends BroadcastReceiver {

public static native void   nativeReceiveLocalNotification (String aID, String aTitle, String aMessage, boolean aOnForeground );

/** This method receives the alarms set by LocalNotificationScheduler,
*   notifies the CAndroidNotifications c++ class, and (if needed) ships a notification banner 
*/
@Override
public void onReceive(Context aContext, Intent aIntent)
{
    Toast.makeText(context, text, duration).show();
}

}

安卓清单:

<receiver android:name="com.category.localnotifications.LocalNotificationReceiver">
        <intent-filter>
            <action android:name="${applicationId}.action.LOCAL_NOTIFICATION" />
            <category android:name="com.category.LocalNotification" />
        </intent-filter>
    </receiver>

Android O 迄今为止还很新。因此,我尝试消化并提供尽可能准确的信息。

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

  • Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.
    • 应用程序可以使用Context.registerReceiver()在运行时注册接收器any广播、天气implicit or explicit.
  • 应用程序可以继续注册显式广播在他们的清单中。

另外,在https://developer.android.com/training/scheduling/alarms.html https://developer.android.com/training/scheduling/alarms.html,这些示例使用显式广播,并且没有提及有关 Android O 的任何特殊内容。


我可以建议您尝试如下显式广播吗?

public static void startAlarmBroadcastReceiver(Context context, long delay) {
    Intent _intent = new Intent(context, AlarmBroadcastReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, _intent, 0);
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    // Remove any previous pending intent.
    alarmManager.cancel(pendingIntent);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, pendingIntent);        
}

报警广播接收器

public class AlarmBroadcastReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
    }

}

In AndroidManifest,只需将类定义为

<receiver android:name="org.yccheok.AlarmBroadcastReceiver" >
</receiver>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

LocalNotification 与 AlarmManager 和 BroadcastReceiver 在 Android O (oreo) 中未启动 的相关文章

随机推荐

  • 核心数据不保存对 Transformable 属性的更改

    我正在保存一个NSMutableArray在我的核心数据存储中的 Transformable 属性中 我可以使用中的数据正确创建实体NSMutableArray然后将其从属性中加载出来 甚至进行更改 当我浏览我的应用程序并重新访问它时 我的
  • 如何在jsp中显示从dao获取的数据

    in jsp table width 100 border 0 cellspacing 2 cellpadding 2 tr class tab highlighted 2 td class tab highlighted 2 width
  • 如何创建php调度进程?

    我被要求创建一个网络应用程序 它将在特定时间内向用户发送更新 例如 2009 年 6 月至 8 月期间 user1 每 5 分钟请求一次更新 user2 仅今天每 10 分钟请求一次更新 从 2009 年 6 月到 8 月 user3 每
  • 当我使用 Tomcat 7 JDBC 连接池取消部署 Web 应用程序时,为什么连接仍然存在?

    我已经将一个最小的 Spring Web 应用程序部署到 Tomcat 7 0 22 它由几个页面 一个控制器 一个服务和一个 DAO 组成 该 DAO 有一个运行 SELECT 查询的方法 Web 应用程序配置为使用新的Tomcat JD
  • 访问带有向量索引的矩阵

    我有一个矩阵 a 16x3 和一个向量 b 16x1 b 显示矩阵 a 中每行有多少个有效值 a magic 3 a 8 1 6 3 5 7 4 9 2 b 1 3 2 b 1 3 2 我想做的是将无效值设置为 NaN a b 1 end
  • 活动滚动时绘制面板边框

    当我尝试制作面板边框时 我遇到了问题 首先我将属性面板设置为 自动滚动 真 然后我将边框绘制代码放在面板事件中 ControlPaint DrawBorder e Graphics ClientRectangle Color Black 5
  • 需要 FLAG_ACTIVITY_NEW_TASK 澄清

    所以我的问题是 我启动 App1 打开 Screen1 然后打开 Screen2 我按 Home 键 将 App1 留在后台 我打开 App2 并启动 App1 Screen1FLAG ACTIVITY NEW TASK https dev
  • 通过 VBA 设置默认打印机

    我正在运行一个在两台打印机之间切换的 Excel 宏 一台名为 RecOffice Pink 另一台名为 RecOffice White 这是解决 VBA 无法轻松指定打印托盘问题的一个巧妙的解决方法 粉红色打印机除了一个纸盘之外的所有纸盘
  • 抓取 Finviz 页面以获取表中的特定值

    首先我要说的是 我不支持抓取服务条款不允许的网站 这纯粹是为了从各个网站假设收集财务数据的学术研究 如果有人想看这个链接 存储在 URLs csv 文件中 想要抓取第 2 5 列 即 Ticker Perf Week Perf Month
  • 使用 csv 文件从元组到整数

    我在使用 csv reader 时遇到了一些问题 我有两个文件 FileA 对应 9 个地理坐标 x 第 1 列 和 y 第 2 列 没有标题 该文件从 Excel 保存到 csv 文件中 301506 5918202 301012 591
  • 防止注销后的返回操作

    注销后 如果我按浏览器中的后退按钮 它不应该显示上一页 它必须转到默认页面 仅限登录页面 所以我尝试了很多方法 Ruby on Rails应用程序 例如 history forward onbeforeunload 元标记中的过期缓存 ht
  • 使用 sed 交换两个单词

    我试图交换一行中的两个单词 但它不起作用 例如 今天是我大学的第一天 应该是 我的今天是大学的第一天 这是我尝试过的 sed s a zA z0 9 a zA z0 9 a zA z0 9 3 2 1 filename txt 我究竟做错了
  • 阻止导入的模块出现在代码完成中?

    如何防止导入的模块被访问 即堵塞我的代码完成选项 例如 testmodule py import os def o stuff return 当我导入时testmodule 我不想os每次我打字时都会出现testmodule 我只想要在 t
  • 基于寄存器的虚拟机如何工作?

    基于寄存器的虚拟机如何工作 我正在寻找有关基于寄存器的虚拟机如何工作的介绍 有人可以帮忙吗 谢谢 具有可用源代码的基于寄存器的 VM 的一个示例是Lua http www lua org 有许多资源可能会有所帮助 Lua 5 0的实现 ht
  • WPF Ribbon - 隐藏选项卡标题(单选项卡应用程序)

    我是 WPF 新手 正在尝试使用功能区控件 我的应用程序中有一个选项卡 并且希望隐藏标题但仍显示选项卡本身 我一直在尝试各种属性和样式 但我只能隐藏整个选项卡 我尝试过 ribbontab 可见性 ribbontab header 可见性
  • 无论如何,Task.Factory.StartNew 在 UI 线程上调用

    我必须处理一个奇怪的问题 至少从我的角度来看是这样 我使用任务来等待变量获取特定值 然后再次在 ui 线程上运行 继续 部分 现在的问题是 在我调用 StartNew 之前 调用内部的 ManagedThreadId 是相同的 它会冻结我的
  • 检查表达式语言中是否存在参数[重复]

    这个问题在这里已经有答案了
  • 如何使用 ObservableObject 更新 UIViewRepresentable

    我正在尝试学习与 SwiftUI 结合 并且正在努力如何更新我的视图 来自 UIKit ObservableObject 之前BindableObject 问题是 显然 方法updateUIView一旦 Published对象发送它已更改的
  • 如何知道我的 SensorManager 是否有已注册的传感器

    我正在为我的 Android 应用程序使用传感器 我用一行代码注册传感器 mySensorManager registerListener this orientationSensor SensorManager SENSOR DELAY
  • LocalNotification 与 AlarmManager 和 BroadcastReceiver 在 Android O (oreo) 中未启动

    我的本地通知在 SDK 26 之前的 Android 上运行 但在 Android O 中 我收到以下警告 并且广播接收器不会被触发 W BroadcastQueue Background execution not allowed rec