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 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);
        }
    }
}

你可能感兴趣的:(android,通知)