Android 在通知栏实现计时功能

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。

原文地址:https://www.cnblogs.com/BobAdmin/p/7929835.html