如何实现Notification中已弃用的方法

2024-01-04

我有一个小问题,但不知道如何摆脱这个问题。

我创建了一个用于提供通知的类,但这些行被标记为已弃用:

...
Notification notification = new Notification(icon, text, time); // deprecated in API level 11
...
notification.setLatestEventInfo(this, title, text, contentIntent); // deprecated in API level 11
...

替代方法是:

...
Notification noti = new Notification.Builder(mContext)
         .setContentTitle("New mail from " + sender.toString())
         .setContentText(subject)
         .setSmallIcon(R.drawable.new_mail)
         .setLargeIcon(aBitmap)
         .build(); // available from API level 11 and onwards
...

我可以写一段代码吗:

if(API_level < 11)
{
...
    Notification notification = new Notification(icon, text, time); // deprecated in API level 11
    ...
    notification.setLatestEventInfo(this, title, text, contentIntent); // deprecated in API level 11
    ...
}

else
{
    ...
    Notification noti = new Notification.Builder(mContext)
             .setContentTitle("New mail from " + sender.toString())
             .setContentText(subject)
             .setSmallIcon(R.drawable.new_mail)
             .setLargeIcon(aBitmap)
             .build(); // available from API level 11 and onwards
    ...
}

我提供的最低 sdk 版本为“8”。

Edit:

我确实喜欢下面的:

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB){

            Notification notification = new Notification(icon, text, time);

            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TaskDetails.class), 0);

            notification.setLatestEventInfo(this, title, text, contentIntent);

            notification.flags |= Notification.FLAG_AUTO_CANCEL;

            mNM.notify(NOTIFICATION, notification);
        } 
        else
        {
            // what to write here
        }

我可以写什么else部分 ??


这就是我最终得出的解决方案:

if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {

            notification = new Notification(icon, text, time);
            notification.setLatestEventInfo(this, title, text, contentIntent); // This method is removed from the Android 6.0
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            mNM.notify(NOTIFICATION, notification);
        } else {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(
                    this);
            notification = builder.setContentIntent(contentIntent)
                    .setSmallIcon(icon).setTicker(text).setWhen(time)
                    .setAutoCancel(true).setContentTitle(title)
                    .setContentText(text).build();

            mNM.notify(NOTIFICATION, notification);
        }

Edit:

上述解决方案有效。尽管如此,自从,NotificationCompat.Builder引入类后,我们可以跳过 if 条件来检查比较当前 API 版本。所以,我们可以简单地删除if...else条件,并与:

NotificationCompat.Builder builder = new NotificationCompat.Builder(
                        this);
notification = builder.setContentIntent(contentIntent)
                      .setSmallIcon(icon).setTicker(text).setWhen(time)
                      .setAutoCancel(true).setContentTitle(title)
                      .setContentText(text).build();
mNM.notify(NOTIFICATION, notification);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何实现Notification中已弃用的方法 的相关文章

  • 如何设置长PreferenceScreen的滚动位置

    Android 应用程序有一些很长的首选项屏幕 它们始终在首选项菜单的顶部打开 我知道用户想要在首选项菜单中的位置 如何强制打开首选项屏幕并滚动到特定的首选项项目 我知道这是一个旧问题 所以这个答案仅供参考 要自动选择给定屏幕 您所要做的就
  • 在 Android 上将视频设置为壁纸

    我想知道如何将视频设置为壁纸 否则不可能 我可以将图像设置为壁纸 并且可以构建动态壁纸 但无法将视频设置为壁纸 所以有人知道我该怎么做吗 提前致谢 我认为唯一可以做到的方法是将其合并到 动态壁纸 中 缺点是正如其他人提到的那样 这会严重影响
  • 如何增加 Gradle 守护进程的最大堆大小?

    签署 apk 时 我收到以下消息 To run dex in process the Gradle daemon needs a larger heap It currently has 1024 MB For faster builds
  • ImageView Android 内存

    我原来的后续question https stackoverflow com questions 5339883 android app ram usage 有没有一种方法可以在 Android 应用程序中使用 ImageViews 而不使
  • 读取 Android 4.2 中的 APN?

    我有个问题阅读 APN在安卓v4 2中 是读 不是写APNS 它抛出一个安全异常 没有写入 APN 设置的权限 用户 10068 和当前用户都没有权限 进程有 android permission WRITE APN SETTINGS 相同
  • Android异步服务调用策略

    这是场景 客户端对服务进行远程调用 返回 void 并提供 回调对象 服务在后台线程上执行一些长时间运行的逻辑 然后使用回调对象来触发以太成功或失败 因为这些操作视觉元素 执行 Activity runOnUiThread 块 该场景运行良
  • android studio更新到3.0后任务执行失败

    当我更新 Android Studio 3 0 时 出现错误 unable to merge with dex 然后我添加了mutiDexEnabled true并且还添加了com android support multidex 1 0
  • 意图过滤器到底是什么?

    我读过很多关于意图过滤器的文章 但我真的无法理解它们到底是做什么的 那么 如果有人可以用一个清晰 的例子向我解释意图过滤器的作用到底是什么 thanks 我认为这是有据可查的here http developer android com g
  • Android/java:从 ProGuard 过渡/迁移到 R8?

    我想知道如何从ProGuard to R8 我是否应该从 Gradle 文件中删除与 Proguard 相关的行并添加android enableR8 true线代替 Thanks Proguard 由 GuardSquare 开发和维护
  • Android:让用户从图库中选择图像或视频

    是否可以以这样的方式启动图库 以便同时显示图片和视频 Thanks 从图库中选择音频文件 Use MediaStore Audio Media EXTERNAL CONTENT URI Intent intent new Intent In
  • Android物理引擎[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 在研究了 3D 游戏编程之后 很快就明白了为什么物理引擎非常有用 Android 支持哪些物理引擎并且可以在 Android 上使用 关于
  • 如何将您的终端与 Android 模拟器连接

    我尝试导航到 android 工具文件夹并输入 adb shell 命令 但它似乎不起作用 我的终端似乎只能识别命令的 adb 部分 并给我一条错误消息 我究竟做错了什么 通过键入列出所有连接的设备adb devices 检查是否列出了任何
  • 错误:类 kotlin.reflect.jvm.internal.FunctionCaller$FieldSetter

    我已尝试一切方法来消除此错误 但它不断出现 Class kotlin reflect jvm internal FunctionCaller FieldSetter can not access a member of class com
  • 为什么 Android 上的免安装应用有两种设置?

    我使用的是运行 Android 11 的 Pixel 3 我发现有 2 种不同的设置可以控制免安装应用的某些方面 设置 应用程序和通知 默认应用程序 打开链接 即时应用程序 即使未安装 也打开应用程序中的链接 切换默认为开 Google P
  • 如何从Android webview下载文件?

    我下面的代码可以很好地加载 url 页面 并且在搜索歌曲后 当我单击下载链接时 它崩溃了 关于如何让下载管理器与网络视图一起工作的教程并不多 我究竟做错了什么 import java io File import android app A
  • 可下载字体例外

    我决定使用可下载字体 https developer android com guide topics ui look and feel downloadable fonts html在我的项目中 IS 按照指南中的建议实施了所有内容 当我
  • 谷歌的Json解析Gson库:JsonElement和JsonObject有什么区别?

    public abstract class JsonElement extends Object 表示 Json 元素的类 它可以是 JsonObject JsonArray JsonPrimitive 或 JsonNull public
  • Android RxJava 2 JUnit 测试 - android.os.Looper 中的 getMainLooper 未模拟 RuntimeException

    我在尝试为正在使用的演示者运行 JUnit 测试时遇到 RuntimeExceptionobserveOn AndroidSchedulers mainThread 由于它们是纯 JUnit 测试而不是 Android 仪器测试 因此它们无
  • 找不到与给定名称“@style/Theme.AppCompat.Light”匹配的资源

    我已经研究这个问题几个小时了 从 github 下载存储库后 任何 xml 文件中的唯一错误是 No resource found that matches the given name style Theme AppCompat Ligh
  • 带有包含布局的导航抽屉布局

    我认为我的问题实际上很简单 但我不知道如何解决 有一个工作导航抽屉 代码如下

随机推荐

  • 当变量调用别名时如何调用别名

    我添加了一个别名 alias anyalias echo kallel 如果我执行 anyalias kallel 它执行echo命令没有任何问题 现在 如果我以这种方式定义一个变量 var anyalias 然后用这样的方式执行 var
  • 是否有更简单的方法使用 ActiveAdmin 创建/选择相关数据?

    假设我有以下模型 class Translation lt ActiveRecord Base has many localizations end class Localization lt ActiveRecord Base belon
  • 访问 Linkedin 私人可播放流的权限 - 500 或 403 错误

    我在使用 Linked In API V2 时遇到了下一个问题 curl X GET header Accet application json header Authorization Bearer
  • 获取iframe的内容

    我正在尝试获取的内容从另一页 另一个页面是另一个网站 我登录了该网站 获取其内容并将其存储在 我如何获取其中的内容进入当前窗口 简短的回答 你做不到 浏览器使用以下限制限制来自不同网站的内容之间的交互同源政策 http en wikiped
  • 如何在flutter中为image.asset添加onClick?

    我在单击时使用了三个图像 这些图像将导航到其他页面 那么我应该如何在这些图像上使用 onClick 我的代码如下 Row children Expanded child Column children
  • 将 spin.js 微调器插入 div 中?

    刚刚发现 spin js 它似乎是一个救星 问题是如何将微调器插入到我的 div 中 我有一个关注按钮 单击该按钮时 我会删除背景图像并当前替换为 loader gif 我怎样才能用 spin js 做同样的事情 我举了一个 jsfiddl
  • ::-ms-thumb 出现在 MS Edge 中的轨道后面

    我创建了一个滑块 In chrome everything is working fine See image below But in MS Edge thumb appears behind track See image below
  • 无法销毁 codeigniter 中的会话

    我想要实现的是一个简单的登录页面 如果用户登录成功 则重定向到主页 否则保留登录页面 我有 1 个名为login 和 1 个型号名为main 当用户单击登录按钮时 将调用login login send
  • 从大字典中替换 DataFrame 中的值的更好方法

    我编写了一些代码 使用字典将 DataFrame 中的值替换为另一个帧中的值 并且它正在工作 但我在一些大文件上使用它 其中字典可能会变得很长 几千双 当我使用这段代码时 它运行速度非常慢 而且在某些地方还出现了内存不足的情况 我有些确信我
  • Box2D 中的 ChainShape

    最近开始学习libgdx 遇到Box2D的CainShape的问题 我的第一个目标是简单地创建一个带有 ChainShape 的盒子 为了实现这一点 我将四个 Vector2 添加到一个数组中 并使用它们创建一个循环 结果取决于数组中的排列
  • Nunit 运行每个测试两次

    我通过 NUnit 运行测试时遇到问题 我不知道为什么 但每个测试运行两次 问题是 在另一台笔记本电脑上 它通常只运行一次 有没有人遇到同样的问题并且知道如何处理 我有同样的问题 就我而言 我同时拥有Visual Studio 扩展 htt
  • 使用 markdown 时如何正确地将多行 xml 片段粘贴到 github wiki

    我正在尝试为我的项目创建一个 github wiki 但我无法将 Spring beans xml 文件中的片段格式化到此 wiki 中 正确的做法是什么 我尝试使用pre tag code但要么它根本不显示 要么它在同一行中显示所有内容
  • 在 Laravel 5 中间件中获取 cookie

    我正在尝试从 Laravel 5 3 中的中间件检索 cookie 但 request gt cookie language 似乎是空的 我猜它只是在中间件运行后设置的 我在某处读到我应该使用 Cookie queued language
  • Qt父机制

    有一个QPushButton in a QWidget click该按钮应该打开另一个QWidget 如下编码 项目 pro QT core gui greaterThan QT MAJOR VERSION 4 QT widgets TAR
  • C 中函数内的函数[重复]

    这个问题在这里已经有答案了 我正在编写与此类似的代码 include
  • 如何在 APL 中使用排名运算符代替each

    I have dummytxt abcdefghijk texttoadd down rfikv 20 30 50 并需要以下输出 defghijk20down defghijk30down defghijk50down 我可以这样做 sc
  • 在iPhone SDK中设置铃声[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 应用程序如何在 iPhone 上设置铃声 目前 API 不支持此功能 因为它可用于将铃声设置为用户不打算设置的内容 如果您希望添加该功能
  • 如何在 Blazor WASM 项目中针对不同类型的客户端(用户/通行证和客户端/秘密)组合多种身份验证方案?

    我有一个带有 Blazor 客户端和 ASP NET Core 服务器的 Blazor WASM 项目 我可以使用以下代码对用户 密码进行身份验证 services AddDefaultIdentity
  • spring-boot 应用程序的外部配置

    我有一个 spring boot 应用程序 我想使用外部配置文件运行它 当我将它作为 jar 运行 带有嵌入式 servlet 容器 时 一切都很好 但我想在外部 servlet 容器 Tomcat 下运行它 这里我遇到了外部配置问题 我尝
  • 如何实现Notification中已弃用的方法

    我有一个小问题 但不知道如何摆脱这个问题 我创建了一个用于提供通知的类 但这些行被标记为已弃用 Notification notification new Notification icon text time deprecated in