Notification(通知)的属性及简单使用

Notification的常用属性

属性说明
icon通知上的图标
tickerText通知上显示滚动的文字
when显示通知时间
flags通知的特性
contentView通知的视图
default默认效果
contentIntent点击通知栏的操作
sound播放的声音

示例代码

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

import com.example.administrator.demolist.R;

import java.util.HashMap;
import java.util.Map;

import turbosnail.cc.MainActivity;
import turbosnail.cc.service.service.download_services.DownloadService;
import turbosnail.cc.service.service.entityClass.FileInfo;

/**
 * 通知工具类
 */
public class NotificationUtil {
    private NotificationManager mNotificationManager = null;            //使用NotificationManager管理通知
    private Map<Integer, Notification> mNotifications = null;        //存储当前所有活动的通知方便为我们管理
    private Context mContext;

    public NotificationUtil(Context context) {
        //获得通知系统服务
        mNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        mNotifications = new HashMap<>();
        mContext = context;
    }

    public void showNotification(FileInfo fileInfo) {
        //判断通知是否已经显示
        if (!mNotifications.containsKey(fileInfo.getId())) {
            //创建通知对象
            Notification notification = new Notification();
            //设置滚动文字
            notification.tickerText = fileInfo.getFileName() + "开始下载";
            //设置显示时间
            notification.when = System.currentTimeMillis();
            //设置图标
            notification.icon = R.drawable.tabbar_me;
            //设置通知特性
            notification.flags = Notification.FLAG_AUTO_CANCEL;  //FLAG_AUTO_CANCEL点击通知栏的内容后自动消失
            //点击通知栏后的操作
            Intent intent = new Intent(mContext, MainActivity.class);
            //包装intent
            PendingIntent pintent = PendingIntent.getActivity(mContext, 0, intent, 0);
            notification.contentIntent = pintent;

//          这里是关键

            //创建RemoteView对象,远程视图   也就是通过RemoteView绑定视图
            RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.item_notification);
            //设置开始按钮操作
            Intent intentStart = new Intent(mContext, DownloadService.class);
            intentStart.setAction(DownloadService.ACTION_START);
            intentStart.putExtra("fileInfo", fileInfo);
            PendingIntent piStart = PendingIntent.getService(mContext, 0, intentStart, 0);
            remoteViews.setOnClickPendingIntent(R.id.btn_start_download, piStart);
            //设置结束按钮操作
            Intent intentStop = new Intent(mContext, DownloadService.class);
            intentStop.setAction(DownloadService.ACTION_STOP);
            intentStop.putExtra("fileInfo", fileInfo);
            PendingIntent piStop = PendingIntent.getService(mContext, 0, intentStop, 0);
            remoteViews.setOnClickPendingIntent(R.id.btn_end, piStop);
            //设置TextView
            remoteViews.setTextViewText(R.id.tv_fileName,fileInfo.getFileName());
            //设置Notification的视图
            notification.contentView = remoteViews;
            //发出通知
            mNotificationManager.notify(fileInfo.getId(), notification);
            //把通知加到集合中
            mNotifications.put(fileInfo.getId(), notification);
        }
    }

    /**
     * 取消通知
     *
     * @param id
     */
    public void cancelNotification(int id) {
        //取消通知
        mNotificationManager.cancel(id);
        //删除集合中的通知
        mNotifications.remove(id);
    }

    /**
     * 更新进度条
     * @param id
     * @param progress
     */
    public void updateNotification(int id, int progress) {
        Notification notification = mNotifications.get(id);
        if (notification != null) {
            //修改进度条
            notification.contentView.setProgressBar(R.id.pb_progress,100,progress,false);
            //重新发送通知
            mNotificationManager.notify(id,notification);
        }
    }
}

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xpq_lrh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值