Notification是APP 向系统发出通知时,它将先以图标的形式显示在通知栏中。用户可以下拉通知栏查看通知的详细信息。我们可以在通知栏实现自定义的效果,也可以结合service和BroadCastReceiver实现推送的效果,下面是在通知栏实现计时器的功能。

首先创造NotificationManager 对象:

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

  然后再创建Builder对象:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.wzzq_logo)
.setContentTitle("标题:时间提示");
builder.setOngoing(true);//注:在这里将builder的onGoing设置为true就实现了点击通知栏不会消失,由于我们要实现的是计数器,就要求该通知要一直显示在通知栏上;

  再创建PendIntent对象:(在这里创建PendIntent对象的话就能够点击通知栏跳转到制定的activity)

 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);

  然后将builder设置到notificationManager里

 builder.setContentIntent(pendingIntent);
notificationManager.notify(serviceId,builder.build());

  这样,整个的Notification部分就完成了。然而我们要实现的是计时器,计时的效果在哪呢?

我把它放在一个service里面了,通过一个线程实现计时效果。

下面是实现整个效果的service:

public class NotificationService extends Service {
private NotificationCompat.Builder builder;
private NotificationManager notificationManager;
private int totalSecond;
private final int serviceId = 0X3; @Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.wzzq_logo)
.setContentTitle("标题:时间提示");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setOngoing(true);
builder.setContentIntent(pendingIntent);
notificationManager.notify(serviceId,builder.build());
startForeground(serviceId,builder.build());
new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0;i<Integer.MAX_VALUE;i++){
totalSecond = PreferencesUtils.getInt(PatrolTimeNotificationService.this, AppConfig.WZZQ_PATROL_SECONDS, 0);//这里是我从项目里面拿到一个时间进行更新
            
if(i > totalSecond + 1){
return;
// notificationManager.cancel(0x3);
}else{
builder.setContentText(getStringTime(totalSecond));
notificationManager.notify(serviceId,builder.build());
}
try {
Thread.sleep(1000);//一秒钟更新一次
} catch (InterruptedException e) {
e.printStackTrace();
}
}
notificationManager.notify(serviceId,builder.build());
startForeground(serviceId,builder.build());
}
}).start();
return super.onStartCommand(intent, flags, startId);
}

//这个方法是把int类型的数据转换成时间格式
private String getStringTime(int cnt) {
int hour = cnt / 3600;
int min = cnt % 3600 / 60;
int second = cnt % 60;
return String.format(Locale.CHINA, "%02d:%02d:%02d", hour, min, second);
} public void stopService(){
this.stopForeground(true);
} }

  

最后,在需要用到的地方将service进行启动就好了

NotificationService notificationService = new NotificationService(); 
Intent intent = new Intent(mContext,notificationService.getClass());
startService(intent);
 

这样就实现了在通知栏显示计时器的功能

别忘了 在manifest文件里面注册这个service。

Android 在通知栏实现计时功能的更多相关文章

  1. Android O 正式版新功能

    ref: Android O新特性和行为变更总结zzhttp://www.cnblogs.com/bluestorm/p/7148134.html Android O正式版带来了诸多新功能,如Tens ...

  2. Unity3D 游戏计时功能实现

    最近工作实在是太忙了,没办法认真写博客,但是还是要好好记录下日常的学习. 需求 各类游戏中都大量运用到计时功能,不管是直接显示的在前端UI,还是后台运行. 思路 Unity中提供了Time类可以方便的 ...

  3. I.MX6 Android 移除 Settings wifi功能

    /********************************************************************* * I.MX6 Android 移除 Settings w ...

  4. Java乔晓松-android中调用系统拍照功能并显示拍照的图片

    android中调用系统拍照功能并显示拍照的图片 如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图 代 ...

  5. Android 实现登录界面和功能实例

    近期一个android小程序须要登录功能,我简单实现了一下.如今记录下来也当做个笔记,同一时候也希望能够相互学习.所以,假设我的代码有问题,还各位请提出来.多谢了! 以下.就简述一下此实例的主要内容: ...

  6. android不知不觉偷拍他人功能实现(手机关闭依然拍照)【申明:来源于网络】

    android不知不觉偷拍他人功能实现(手机关闭依然拍照)[申明:来源于网络] 地址:http://blog.csdn.net/huangxiaoguo1/article/details/536660 ...

  7. 用Eclipse编写Android程序的代码提示功能

    用Eclipse编写Android程序的代码提示功能主要是在java和xml文件中,有时候会失效,默认的提示功能有限. 1)java文件自动提示     Window->Preferences- ...

  8. C/C++/Java 程序计时功能函数

    编写程序肯定要使用计时功能,来判断程序的执行时间.今天Google了一下,自己就梳理总结一下: (1)C/C++程序计时 C/C++中使用的计时函数是clock(). C语言中的头文件对应是#incl ...

  9. 【cocos2d-x制作别踩白块儿】第九期:游戏计时功能(附源代码)

    游戏没有计时,不是坑爹吗? 这一期,我们将来加入游戏计时功能. 1. 定义变量和函数 我们先在HelloWorldScene.h中定义几个变量和函数 long startTime; bool time ...

随机推荐

  1. python学习之第一课时--初始python

    Python前世今世 python是什么 python是一门多种用途的编程语言,时常在扮演脚本语言的角色 python流行原因 软件质量 提高开发者效率(python代码大小为C/java的1/3-1 ...

  2. C++点滴20130802

    1.sprintf与printf,fprintf为三兄弟.其中printf输出到屏幕,fprintf输出到文件,而sprintf输出到字符串中.通常情况下,屏幕是可以输出的,文件也可以写的(除非磁盘满 ...

  3. tp5引入第三方类库

    1.在/public/index.php中添加 define('EXTEND_PATH', '../extend/'); 2./extend/lib 中添加第三方类,类文件的名称和类名一样,命名空间为 ...

  4. 2017年最受欢迎的UI框架

    前端领域最近几年发展的特别迅速,可以说是百家争鸣.在底层的前端框架领域中,最早是jquery称霸互联网,近两年MVVM类型的框架慢慢成为主流,Vue.React和Angular三大框架并驾齐驱.可以说 ...

  5. css中居中方法小结

    ---恢复内容开始--- 1.文字垂直居中 .header_nav-item{ height:38px; line-height:38px; } 即文字所在模块的高度和行高设置成一样的! 2.块元素垂 ...

  6. CentOs 系统启动流程相关

    CentOS的启动流程 1)加载BIOS 的硬件信息,获取第一个启动设备 2)读取第一个启动设备MBR 的引导加载程序(grub) 的启动信息 3)加载核心操作系统的核心信息,核心开始解压缩,并尝试驱 ...

  7. Element ui表格展示图片问题

    当需要遍历图片时,不能直接使用prop绑定值,具体 代码如下 <el-table-column label="头像" width="100"> &l ...

  8. 基于Vue.js的大型报告页项目实现过程及问题总结(一)

    今年5月份的时候做了一个测评报告项目,需要在网页正常显示的同时且可打印为pdf,当时的技术方案采用jquery+template的方式,因为是固定模板所以并没有考虑报告的模块化区分,九月底产品提出新的 ...

  9. Spring框架学习之高级依赖关系配置(二)

    紧接着上篇内容,本篇文章将主要介绍XML Schema的简化配置和使用SpEL表达式语言来优化我们的配置文件. 一.基于XML Schema的简化配置方式 从Spring2.0以来,Spring支持使 ...

  10. ajax跨域请求解决方案

    大家好,今天我们学习了js的跨域请求的解决方案,由于JS中存在同源策略,当请求不同协议名,不同端口号.不同主机名下面的文件时,将会违背同源策略,无法请求成功!需要进行跨域处理! 方案一.后台PHP进行 ...