Android Notification的常规使用方法笔记

本文深入探讨了Android通知系统的实现机制,从通用通知使用方法到API级别的详细解析,包括四种不同风格样式(MediaStyle、BigPictureStyle、InboxStyle、BigTextStyle),并介绍了如何自定义通知。此外,还解释了从Android4.1开始通知系统演变的过程,以及如何利用Notification.Builder进行更灵活的定制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 第一种:通用通知使用方法,但已被抛弃

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn);
        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
        // 创建延迟意图
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, NotificationTwoActivity.class), 0);
        // 通用通知使用方法,但已被抛弃
        Notification notification = new Notification();
        // 当有通知时,屏幕最上方自动弹出的提示语,2秒后自动消失
        notification.tickerText = "有新消息,点我"; 
        // 该设置不起效果,通知任然是ic_launcher图标,但如果不设置该属性通知不起作用
        notification.icon = R.drawable.aaa;
        // 该设置不起效果,
        notification.when = System.currentTimeMillis();
        notification.setLatestEventInfo(this, "title", "contentText", pendingIntent);
        // 该设置不起效果,
        notification.number = 1;
        notification.flags = Notification.FLAG_AUTO_CANCEL; // 点击后自动清除通知
        // 通过通知管理器来发起通知
        manager.notify(NOTIFICATION_FLAG, notification);
    
    
        btn.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // 清除id为NOTIFICATION_FLAG的通知  
                manager.cancel(NOTIFICATION_FLAG);
                // 清除所有通知
                // manager.cancelAll();
            }
        });
    }
    
  • 第二种:API11及之后可用

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.btn);
    manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // 创建延迟意图
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, NotificationTwoActivity.class), 0);
    
    //API 11及之后可用
    Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.drawable.aaa)
            .setTicker("有新消息,点我")
            .setContentTitle("title")
            .setContentText("contentText")
            .setNumber(1)
            .setContentIntent(pendingIntent)
            // 注意build()是在API level 16及之后增加的,在API11中可以使用getNotificatin()来代替  
            .getNotification();
    
    notification.flags = Notification.FLAG_AUTO_CANCEL; // 点击后自动清除通知
    // 通过通知管理器来发起通知
    manager.notify(NOTIFICATION_FLAG, notification);
    
    btn.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
            // 清除id为NOTIFICATION_FLAG的通知
            manager.cancel(NOTIFICATION_FLAG);
            // 清除所有通知
            // manager.cancelAll();
          }
        });
     }
    
  • 第三种:API16及之后可用
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.btn);
    manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // 创建延迟意图
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, NotificationTwoActivity.class), 0);
    
    // API16及之后可用
    Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.drawable.aaa)
            .setTicker("有新消息,点我")
            .setContentTitle("title")
            .setContentText("contentText")
            .setNumber(1)
            .setContentIntent(pendingIntent)
            .build();
    
    notification.flags = Notification.FLAG_AUTO_CANCEL; // 点击后自动清除通知
    // 通过通知管理器来发起通知
    manager.notify(NOTIFICATION_FLAG, notification);
    
    btn.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
            // 清除id为NOTIFICATION_FLAG的通知
            manager.cancel(NOTIFICATION_FLAG);
            // 清除所有通知
            // manager.cancelAll();
        }
    });
    }
    
  • 第四种:自定义通知
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.btn);
    manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // 创建延迟意图
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, NotificationTwoActivity.class), 0);
    
    // API16及之后可用
    Notification notification = new Notification();
    notification.icon = R.drawable.aaa;
    notification.tickerText = "自定义,有新消息,点我";
    notification.when = System.currentTimeMillis();
    
    RemoteViews rv = new RemoteViews(getPackageName(), R.layout.remote);
    rv.setTextViewText(R.id.tv, "haha,你是我的小苹果");
    
    notification.contentView = rv;
    notification.contentIntent = pendingIntent;
    
    notification.flags = Notification.FLAG_NO_CLEAR; // 点击后不自动清除通知
    // 通过通知管理器来发起通知
    manager.notify(NOTIFICATION_FLAG, notification);
    
    btn.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
            // 清除id为NOTIFICATION_FLAG的通知
            manager.cancel(NOTIFICATION_FLAG);
            // 清除所有通知
            // manager.cancelAll();
        }
    });
    }
    

从Android 4.1开始,Notification可以支持展开显示的模式,这样一来,Notification就演变出了下面4种不同的风格样式(builder.setStyle()):
- MediaStyle
- BigPictureStyle
- InboxStyle
- BigTextStyle

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值