Android使用通知
来源:互联网 发布:淘宝下拉框软件 编辑:程序博客网 时间:2024/06/16 15:10
首先是获取NotificationManager的实例:
NotificationManager manager = (NotificationManger) getSystemService(Content.NOTIFICATION_SERVICE);
然后用一个Builder构造器来创建Notification对象:
Notification notification = new NofiticationCompat.Builder().builder;//空的notification对象
可以这样:
Notification notification = new NofiticationCompat.Builder()
.setContentTile(" ")//设置通知名称
.setContentText(" ");//设置通知内容
.setWhen(System.CurrentTimeMillis())//显示通知发出时间
.setSmallIcon(R.drawable.small_icon)//设置通知显示在状态栏上的小图标
.setBigIcon(BitmapFactory.decodeResource(getResources(),R.drawable.large_icon))//............大图标
发送一个通知的Button:
@Override public void onClick(View view) { switch (view.getId()){ case R.id.send_notice: NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new NotificationCompat.Builder(this) .setContentTitle("This is content title") .setContentText("This is content text") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .build(); manager.notify(1,notification); break; default: break; } }
设置通知的点击效果:
新建一个通知的活动NotificationActivity:
Intent intent = new Intent(this,NotificationAcitivity.class); PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
然后再在实例化notification的时候加入.setContentIntent(pi);
设置点击后取消通知:
①在实例化notification的时候加入.setAutoCancel(true)
②在Notification OnCreate()中加入代码:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.cancel(1);//1是指的这条通知的id
.setSound(Uri.fromFile(new File("路径"))) 通知时播放音频
.setVibrate(new long[]{0,1000,2000,3000}) 设置通知时震动 0表示静止0ms 1000表示震动1000ms 2000表示静止2000ms 3000表示又震动3000ms 以此类推
震动的权限声明: <uses-permission android.name="android.permission.VIBRATE">
.setLights(Color.GREEN,1000,1000) 设置LED灯 第一个参数是颜色,第二个是亮的时长,第三个是暗的时长
.setDefaults(NotificationCompat.DEFAULT_ALL) 设置默认效果 这里是全部默认
通知的高级功能:
设置长文本的text
.setStyle(NotificationCompat.BigTextStyle().bigText("Learn how to build notifications, send and sync data, and use voice actions.Get the officical Android IDE and developer tools to build apps for android."))
插入图片
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)))
设置通知的重要程度
.setPriority(NotificationCompat.PRIORITY_MAX) 这里把通知的重要级设置为最高
- android通知的使用
- Android 通知的使用
- Android 通知使用小结
- Android 使用Notification(通知)
- android Notification通知使用
- Android使用通知
- android中通知的使用
- android通知(Notification)的使用
- android通知栏使用汇总
- Android:通知栏的使用
- android Notification(通知) 的使用
- Android 通知栏的使用
- Android中通知的使用
- Android 通知 Notification 使用详情
- Android通知Notification的使用
- android 使用通知(Notification)
- Android Notification(一)使用通知
- Android:通知栏的使用
- 定义平面中的一个Circle类, 1编写一个无参数构造函数, 2编写一个有参的构造函数 3在主函数中调用无参构造函数生成圆的实例c1,调用有参构造函数生成圆的实例c2,调用实例方法判断c1和c2是否重
- JAVA课程1用NPP写HW
- 二叉树的基本操作(插入、查找)
- class Introduction to Databases in Python
- java初级入门
- Android使用通知
- 在 Golang 中开发中使用 Makefile
- 遍历二叉树
- c-风格字符串和string类型的字符串的异同
- Spring Data JPA进阶-@Query注解
- Android studio中布局文件出现render problem问题
- 【CSS】学习笔记
- 二叉树操作(删除)
- hdu1232(基础并查集)