Android 开发之 Notification

调用步骤:

  • 调用getSystemService(NOTIFICATION_SERVICE)方法获取系统的 Notification Manager服务
  • 通过构造器创建一个Notification对象
  • 为Notification设置各种属性
  • 通过NotificationManager发送Notification

Notification常用方法

        /*设置large icon*/
        .setLargeIcon(bitmap)
         /*设置small icon*/
        .setSmallIcon()
        /*设置title*/
        .setContentTitle()
        /*设置详细文本*/
        .setContentText()
         /*设置发出通知的时间为发出通知时的系统时间*/
        .setWhen(System.currentTimeMillis())
         /*设置发出通知时在status bar进行提醒*/
        .setTicker("设置状态栏提示文本")
        /*setOngoing(boolean)设为true,notification将无法通过左右滑动的方式清除 * 可用于添加常驻通知,必须调用cancle方法来清除 */
        .setOngoing(true)
         /*设置点击后通知消失*/
        .setAutoCancel(true)
         /*设置通知数量的显示类似于QQ那种,用于同志的合并*/
        .setNumber(2)
         /*点击跳转Activity*/
        .setContentIntent();

示例代码

 imageToast=(Button) findViewById(R.id.imageToast);
        imageToast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count++;
                NotificationManager manager =(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);

                builder.setSmallIcon(R.mipmap.ic_launcher);
                builder.setContentTitle("你有"+count+"个新消息");//设置状态栏题目
                builder.setContentText("设置状态栏文本");
                builder.setTicker("设置状态栏提示文本");
                builder.setNumber(2);//设置消息数目

                Notification notification  = builder.build();
                manager.notify(NOTIFICATION_ID,notification);
            }
        });

转载于:https://my.oschina.net/617669559/blog/918215

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值