notification使用

notification基本使用


1.什么是notification?

notification就是通知,展示在屏幕的顶端,首先会表示一个图标的形式,当用户向下滑动时候,展示出通知具体内容。

注意:Android3.0之后,notification的变化比较大。如果要考虑兼容3.0以下的手机版本,需要使用V4或者V7包下Notification。


2。notification使用

一.基本使用。使用V4或者V7下的NotificationCompat做到向下兼容
【注意】
在5.0之后的系统中,官方推荐将通知的图标设置为透明的图标
二、大文本通知
BigTextStyle
【注意】
大文本通知只有处于通知栏顶部时,才会显示大文本。否则就是一个普通的通知
三、大图通知
BigPictureStyle
【注意】
大图通知只有处于通知栏顶部时,才会显示图片。否则就是一个普通的通知
四、进度条通知
setProgress
notificationManager.notify方法可以在子线程中执行
五、自定义通知


3,notification基本使用代码如下
1.获取notificationmanager对象(getSystemService获取系统服务)
2.创建一个notification对象(创建通知的构建者对象)
3.发送通知的提交。
    private NotificationManager mNotificationManager;
 //1、获取NotificationManager对象
        //getSystemService获取系统服务
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //2、创建一个Notification对象
        //创建通知的构建者对象
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this);
        //设置图标:如果没有设置图标,则通知不会出现
        builder.setSmallIcon(R.drawable.ic_tab_category_normal);
        //设置是否显示时间
        builder.setShowWhen(true);
        //设置标题
        builder.setContentTitle("通知的标题");
        //设置内容
        builder.setContentText("这是通知的内容");
        //设置通知的个数
        builder.setNumber(counter++);

        Notification notification = builder.build();
        //3、发送(提交)通知
        //参数1:通知的ID,标识.
        //参数2:Notification对象
        //注意:如果发送多个通知,但是通知的ID一样,则不会显示多条通知;
        // 如果需要显示多条通知,则需要修改ID为不一样
        mNotificationManager.notify(counter,notification);

//大文本显示 
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle("标题");
        builder.setContentText("这是内容简介");
        builder.setSmallIcon(R.drawable.ic_night_on);

        //显示大文本
        NotificationCompat.BigTextStyle bigTextStyle =
                new android.support.v4.app.NotificationCompat.BigTextStyle(builder);
        bigTextStyle.setSummaryText("xxxx");
        bigTextStyle.bigText("ddddd");
        mNotificationManager.notify(id++,bigTextStyle.build());

**
 * 进度条通知
 */
public class MainActivity extends AppCompatActivity {

    private NotificationManager notificationManager;
    private int progress ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

    public void click(View view) {
        //创建通知
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentText("简介");
        builder.setContentTitle("标题");
        //设置进度
        //参数1:总进度
        //参数2:当前进度
        //参数3:布尔类型,true表示总进度不确定,false表示总进度确定(即参数1有效)
        builder.setProgress(100,progress,false);

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (progress < 100) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.d("androidxx", "run: " + progress);
//                progress++;
                    builder.setContentText(progress + "%" );
                    builder.setProgress(100,progress++,false);
                    notificationManager.notify(1,builder.build());
                }
            }
        }).start();
    }

 //大图
        //android.os.Build.VERSION.SDK_INT表示当前手机系统版本号
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            Notification.Builder builder = new Notification.Builder(this);
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setContentTitle("标题");
            builder.setContentText("简介");
            Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle(builder);
            //将图片资源文件转换成Bitmap对象
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a3);
            bigPictureStyle.bigPicture(bitmap);
            bigPictureStyle.setBigContentTitle("XXXXXX");
            bigPictureStyle.setSummaryText("sssssssssssss");
            mNotificationManager.notify(1,bigPictureStyle.build());
        } else {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setContentTitle("标题");
            builder.setContentText("简介");
            NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle(builder);
            //将图片资源文件转换成Bitmap对象
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a3);
            bigPictureStyle.bigPicture(bitmap);
            mNotificationManager.notify(2,bigPictureStyle.build());
        }

        /**
         * 参数1:上下文对象
         * 参数2:请求码,可以是任意的整数
         * 参数3:intent对象
         * 参数4:PendingIntent的类型
         * FLAG_ONE_SHOT表示PendingIntent只能执行一次
         * FLAG_CANCEL_CURRENT表示会删除已存在的事件,用新的事件替代
         */
        PendingIntent pendingIntent = PendingIntent.getActivity(this,1,intent,PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setContentIntent(pendingIntent);

        notificationManager.notify(1,builder.build());



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值