zoukankan      html  css  js  c++  java
  • Android NotificationManager与Notification(通知栏) 的使用

    有时候我们在后台运行程序,但是需要给用户一个提示,在这个时候就需要使用提示信息了,即在提示栏显示一个图标或者是文字提醒用户。下面是实现的代码:

    代码
    protected void showNotification(int id) {
    CharSequence from
    = "定位服务";
    CharSequence message
    = "正在运行";

    Intent intent
    = new Intent();

    PendingIntent contentIntent
    = PendingIntent.getActivity(this, 0,
    intent,
    0);


    notif.icon
    = id;
    notif.setLatestEventInfo(
    this, from, message, contentIntent);


    NotificationManager nm
    = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.notify(R.string.app_name, notif);


    }

    其中ID表示的是图片的ID,文字在这里没有添加,使用的时候按照添加图标的方式可以自己添加!

    这样可以更改图片。

    下面这个是删除提示的代码:

    void delenot() {
    NotificationManager notificationManager
    = (NotificationManager) this
    .getSystemService(NOTIFICATION_SERVICE);
    notificationManager.cancel(R.string.app_name);
    }

    这样我们添加的提示通知,可以清除掉,如果我们不想让系统清除掉,那么需要设置相关的属性:

    notif.flags

    这个属性可以设置为不清除或者是加入正在运行的列表。

  • 相关阅读:
    Git 版本导致 clone 故障
    ELK-Stack 最后一次全篇文档
    Elasticsearch 搜索引擎
    Yum -y update 报错
    Linux OOM 自动杀死进程
    MySQL 执行 'use databases;' 时很慢
    DRBD 数据镜像软件介绍
    ELK 日志管理系统,再次尝试记录
    ELK 日志管理系统,初次尝试记录
    iframe与include的区别
  • 原文地址:https://www.cnblogs.com/fly_binbin/p/1927494.html
Copyright © 2011-2022 走看看