Android Notification通知专题

来源:互联网 发布:工程进度网络图软件 编辑:程序博客网 时间:2024/05/16 03:23
可以直接使用的一个通知
private void notification() {
    //第一步:获取状态通知栏管理:
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    //第二步:实例化通知栏构造器NotificationCompat.Builder
    Notification.Builder builder = new Notification.Builder(mContext);
    builder.setAutoCancel(false);
    builder.setSmallIcon(R.mipmap.title_logo);
    Notification notification = builder.getNotification();
    notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing""正在运行"组中
    notification.flags = Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    builder.setContentTitle(mContext.getString(R.string.app_name));
    if (WearableListener.STATE_CONNECTED == WearableManager.getInstance().getConnectState()) {
        builder.setContentText(String.valueOf(mContext.getString(R.string.btl_connected)));
    } else {
        builder.setContentText(String.valueOf(mContext.getString(R.string.btl_not_connected)));
    }
    builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);//int falgs设置为PendingIntent.FLAG_ONE_SHOT时,通知栏点击事件只能调用一次
    builder.setContentIntent(pIntent);
    manager.notify(R.mipmap.title_logo, builder.build());
}

几种常用的flags:

1.FLAG_AUTO_CANCEL:在用户查看通知信息后自动关闭通知;

2.FLAG_INSISTENT:在用户响应之前一直重复;

3.FLAG_ONGOING_EVENT:放置在“正在运行”栏目中,表示程序正在运行,可见状态,或是后台运行;

4.FLAG_NO_CLEAR:查看通知后不会自动取消,对一直进行中的通知非常有用。

本人不善言词,所以就不多说什么了!
这段代码是,从项目中提出来的,可以直接使用,只要替换里面的string文件和图片即可
0 0
原创粉丝点击