Android学习笔记—— 十 、多媒体的基础使用 - 通知的基础使用方法

  1. 创建通知
		//使用getSystemService(Context.NOTIFICATION_SERVICE)方法获取通知管理器
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        //配置一个PendingIntent对象作为setContentIntent方法的参数
        Intent intent = new Intent(this,DetailActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
        
        //通过Buider构造一个通知对象
        Notification notification = new NotificationCompat
                //设置通知渠道id为11
                .Builder(this, "11")
                //设置通知标题
                .setContentTitle("FBI Warning!")
                //设置通知内容
                //.setContentText("Learn how to build notifications, send and sync data, and use voice action. Get the official Android IDE and developer tools to build apps for Android.")
                //以毫秒为单位获取当前系统时间
                .setWhen(System.currentTimeMillis())
                //设置在通知栏显示的小图标
                .setSmallIcon(R.mipmap.ic_launcher)
                //设置显示在通知的大图标
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                //设置延时Intent用于在用户点击通知时启动对应的组件
                .setContentIntent(pendingIntent)
                //设置用户点击通知后自动取消通知
                .setAutoCancel(true)
                //设置发出通知时播放的提示音
                .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg")))
                //设置手机在收到通知时震动提醒,使用一个长整型的数组控制震动时长和静止时长,第一个数值为静止时长,第二个数值为震动时长以此类推,单位为毫秒
                .setVibrate(new long[]{0, 500, 1000, 500})
                //设置当该通知未读时,锁屏状态下呼吸灯的闪烁颜色、亮起时间和熄灭时间,单位为毫秒
                .setLights(Color.RED,1000,1000)
                //设置让通知显示长文本内容
                .setStyle(new NotificationCompat.BigTextStyle().bigText("Learn how to build notifications, send and sync data, and use voice action. Get the official Android IDE and developer tools to build apps for Android."))
                //设置让通知显示一张图片
                .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)))
                //设置通知的重要程度,共有五级min最小,max最大
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .build();

        //使用notify方法显示通知,同时设置该通知id为1
        notificationManager.notify(1,notification);
  1. 手动取消通知
    如果在创建时没有设置自动取消通知的话可以在想要取消通知时调用cancel方法手动取消通知
				NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
				//这里的参数就是创建通知时设置的id
                notificationManager.cancel(1);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值