写android通知的时候发现Notification的setLatestEventInfo被弃用,于是搜素并整理了一下新的android通知的基本用法。

一、获取NotificationManager实例

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

二、创建Notification实例

在这里需要根据project的min-sdk来选择实现方法,MIN API Level < 11的可以使用setLatestEventInfo()方法,以下介绍API Level 11 之后的Notification实例获取方法。

1. MIN API Level < 16 构建Notification实例的方法

1) 创建Notification.Builder实例

      Notification.Builder builder = new Notification.Builder(context)       .setAutoCancel(true) //设置点击通知后自动取消通知      .setContentTitle("title") //通知标题      .setContentText("describe") //通知第二行的内容      .setContentIntent(pendingIntent) //点击通知后,发送指定的PendingIntent      .setSmallIcon(R.drawable.ic_launcher); //通知图标,必须设置否则通知不显示

2) 调用Notification.Builder的getNotification()方法获得Notification 

                     notification = builder.getNotification();

2. MIN API Level >=16 构建Notification实例的方法

            Notification notification = new Notification.Builder(context)            .setAutoCancel(true)            .setContentTitle("title")            .setContentText("text")            .setSmallIcon(R.mipmap.ic_launcher)            .setContentIntent(pendingIntent)            .build();

三、发送通知

                          notificationManager.notify(1,notification);

以上就是对Android 通知栏的知识资料整理,后续继续补充,谢谢大家对本站的支持。

更多相关文章

  1. android颜色设使用方法
  2. android 点击返回键退出程序的方法
  3. Android studio+真机 运行报错[INSTALL_FAILED_INSUFFICIENT_STO
  4. Android——Android Studio开发环境搭建及设置本地Gradle方法
  5. 在android中创建bitmap避免内存不足的方法
  6. Android Studio使用Wifi调试的方法:使用Android WIFI ADB
  7. 【Android动态布局】之【使用addView方法时,如何保持已有动态控件
  8. Android禁止锁屏,保持常亮方法

随机推荐

  1. Android(安卓)studio 混合编程(一)
  2. android:windowSoftInputMode属性使用
  3. Android(安卓)短信数据库详细总结分析
  4. Android(安卓)中 PopupWindow的用法 汇总
  5. 【Android】MTK Android(安卓)编译命令
  6. Android(安卓)新手常见的10个误区(上)
  7. 类程序Log4j Android
  8. Android(安卓)Studio 如何导入.so文件/ja
  9. Android中的数据结构解析(四)SparseArray和
  10. Android上传文件之FTP