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. 解决 Win10 UWP 无法使用 ss 连接

    一旦使用了 ss, 那么很多应用就无法连接网络. 本文提供一个方法可以简单使用ss提供的代理. 多谢 wtwsgs 提供方法:http://blog.csdn.net/wtwsgs/article/d ...

  2. sublime Text 正则替换

    我遇到一个文章,需要把所有的 (数字) 换为 [数字] 于是我使用 Sublime Text的替换 首先,我们需要打开正则使用"Alt+R" 或打开"Ctrl+h&quo ...

  3. JavaScript HTML Dom改变HTML

    本人为小白,首次写博客  有不正确的地方望多多指点与见谅!! 一,改变HTML的内容 语法: document.getElementById(id).innerHTML=new HTML: 具体用法: ...

  4. PHP中如何定义类及其成员属性与操作

    1.类的定义: i. 类的关键字定义使用class 1.定义一个空类 Class Person{}; 2.定义一个有成员属性和操作的类 Class Person{ 成员属性........ 操    ...

  5. admin的基础配置

    admin自定义配置 一.admin.py 我们知道在models.py文件中创建的数据表,一方面我们可以通过视图函数对其进行增删改查,一方面我们也可以通过admin进行,通常我们是通过admin的前 ...

  6. Java随机数和UUID

    Java随机数和UUID Java随机数 在Java项目中通常是通过Math.random方法和Random类来获得随机数,前者通过生成一个Random类的实例来实现. 此类产生的是一组伪随机数流,通 ...

  7. LeetCode 229. Majority Element II (众数之二)

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  8. 走进 Xamarin Test Recorder for Xamarin.Forms

    此篇是承接之前 走进 UITest for Xamarin.Forms 的,所以如果没有看过之前的可以先看下之前的 UITest 比起上一篇纯敲代码只适合程序员的 UITest ,这一篇不管是程序员还 ...

  9. JavaScript面向对象中的继承

    1.1继承的基本概念 使用一个子类,继承另一个父类,那么子类可以自动拥有父类中的所有属性和方法,这个过程叫做继承. >>>继承的两方,发生在两个类之间. 实现继承的三种方式: 扩展O ...

  10. 【计算机网络】 一个小白的网络层学习笔记:总结下IP,NAT和DHCP

    前言:这篇文章是学习网络层协议时候总结的笔记,前面的主要部分介绍的都是IP协议, 后半部分介绍NAT协议和DHCP协议 参考书籍 <计算机网络-自顶向下>       作者 James F ...