当前位置: 首页 > news >正文

Android Notification 通过增加addAction 跳转回Service重新执行逻辑

1.场景描述

    在App内升级过程中,apk下载过程中网络波动导致连接超时,下载失败后Service生命周期结束。前台通知也被清除。

2.解决思路

    在通知栏中增加重试按钮重启下载服务。

3.代码

  NotificationManager mNotificationManager = (NotificationManager)   getSystemService(Context.NOTIFICATION_SERVICE);Builder mBuilder = new Builder(this);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME,                NotificationManager.IMPORTANCE_DEFAULT);mNotificationManager.createNotificationChannel(channel);mBuilder.setSmallIcon(R.mipmap.ic_launcher).setContentTitle(title).setContentText(text).setChannelId(CHANNEL_ID).setTicker(ticker);} else {mBuilder.setSmallIcon(R.mipmap.ic_launcher).setContentTitle(title).setContentText(text).setTicker(ticker);}Intent intentService = new Intent(this, ApkDownloadService.class);intentService.putExtra("updateInfo", mAppUpdateResponse);intentService.putExtra("updateType", 1);PendingIntent pendingIntent = PendingIntent.getService( this, 0, intentService, PendingIntent.FLAG_IMMUTABLE);mBuilder.addAction(R.mipmap.ic_launcher, getString(R.string.retry), pendingIntent);mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

4.效果图

http://www.dtcms.com/a/270975.html

相关文章:

  • 沃丰科技海外客服系统综合解决方案
  • 36氪专访丨乐橙CEO谢运:AI科技下的业务创新与长期主义下的品牌坚守
  • CD47.【C++ Dev】list的模拟实现(2)
  • RAG实战之dify源码文件解析-pdf文件解析流程
  • STM32F103ZET6的USART 中断配置详解
  • 自动化测试报告优化:jenkins+jmeter定制化HTML报告生成
  • 【LeetCode】大厂面试算法真题回忆(102)--集五福
  • Java学习第二十七部分——bug检修
  • 前端接收流式数据demo,并用markdown解析数据,包括EventSource和fetch两种方式
  • LeetCode 138题解 | 随机链表的复制
  • 力扣 hot100 Day39
  • 【保姆级喂饭教程】Windows下安装Git Flow
  • 电网的智能觉醒——人工智能重构能源生态的技术革命与公平悖论
  • JAVA策略模式demo【设计模式系列】
  • 自动化Trae Apollo参数解释的批量获取
  • 苍穹外卖项目日记(day04)
  • ASP.NET Core 8 轻松配置Serilog日志
  • 智慧码头船舶网络部署5G工业路由器无人值守场景应用
  • 无人设备遥控器之双向通讯技术篇
  • 【机器人】Aether 多任务世界模型 | 4D动态重建 | 视频预测 | 视觉规划
  • C++并发编程-11. C++ 原子操作和内存模型
  • Linux驱动学习day20(pinctrl子系统驱动大全)
  • Ubuntu防火墙缺失问题(unit firewalld.service could not be found, ubuntu 22)
  • EFK9.0.3 windows搭建
  • Linux系统管理实战:生成大文件与定位磁盘挂载点
  • 专题:2025母婴行业洞察报告|附60+份报告PDF汇总下载
  • Linux中shell(外壳)和内核(kernel)的关系
  • Claude Code:终端上的 AI 编码助手,潜力与挑战并存
  • 从零用java实现 小红书 springboot vue uniapp(13)模仿抖音视频切换
  • 华为数通HCIA vs HCIP:新手入门选哪个更合适?