android实现状态栏添加图标的函数实例

作者:pgalxx 时间:2022-11-19 06:57:42 

本文实例讲述了android实现状态栏添加图标的函数。分享给大家供大家参考。具体如下:


private void showNotification() {
// 创建一个NotificationManager的引用
NotificationManager notificationManager = (NotificationManager)
 AutoFile.this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
// 定义Notification的各种属性
Notification notification = new Notification(R.drawable.dvd,
 "天籁之音播放器", System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中
notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.ledARGB = Color.BLUE;
notification.ledOnMS = 5000;
// 设置通知的事件消息
CharSequence contentTitle = "天籁之音正在播放……"; // 通知栏标题
CharSequence contentText = "ameyume"; // 通知栏内容
Intent notificationIntent = new Intent(AutoFile.this, MyPlayerService.class); // 点击该通知后要跳转的Activity
PendingIntent contentItent = PendingIntent.getActivity(AutoFile.this, 0,
 notificationIntent, 0);
notification.setLatestEventInfo(AutoFile.this, contentTitle, contentText,
 contentItent);
// 把Notification传递给NotificationManager
notificationManager.notify(0, notification);
}

要删除图标,用以下代码:


// 启动后删除之前我们定义的通知
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(0);

希望本文所述对大家的Android程序设计有所帮助。

标签:android,状态栏,图标
0
投稿

猜你喜欢

  • Android登录注册功能 数据库SQLite验证

    2023-10-01 20:58:55
  • 超简单C#获取带汉字的字符串真实长度(单个英文长度为1,单个中文长度为2)

    2021-11-13 20:37:55
  • C#游戏开发之实现贪吃蛇游戏

    2023-01-28 01:48:48
  • socket编程时的发送与接收数据时的问题解析

    2022-12-31 03:57:28
  • 经典排序算法之冒泡排序(Bubble sort)代码

    2021-06-08 06:06:18
  • C++实现LeetCode(5.最长回文子串)

    2023-07-03 06:07:13
  • 浅析Android手机卫士之抖动输入框和手机震动

    2022-11-23 08:01:50
  • Android​短信验证码倒计时验证的2种常用方式

    2022-06-23 17:27:42
  • Spring Boot2.0 @ConfigurationProperties使用详解

    2021-06-11 17:21:30
  • Android自定义视图中图片的处理

    2023-06-29 12:07:27
  • 零基础写Java知乎爬虫之获取知乎编辑推荐内容

    2023-11-29 04:33:39
  • 使用GSON库将Java中的map键值对应结构对象转换为JSON

    2022-10-21 14:51:47
  • springmvc之获取参数的方法(必看)

    2023-12-20 09:25:59
  • JavaWeb文件上传下载功能示例解析

    2021-09-20 08:28:58
  • 解决Mybatis-plus和pagehelper依赖冲突的方法示例

    2022-06-28 16:52:59
  • Java 数据结构与算法系列精讲之二叉堆

    2022-05-14 06:31:15
  • Android集成zxing扫码框架功能

    2021-09-27 02:37:17
  • Android 高德地图之poi搜索功能的实现代码

    2022-03-07 01:36:52
  • C#算法之罗马数字转整数

    2022-05-25 16:20:26
  • Android极光推送处理message遇到的坑解决

    2022-08-08 00:49:32
  • asp之家 软件编程 m.aspxhome.com