Android Notification.setLatestEventInfo弃用和替代方法

本文介绍Android6.0之后由于setLatestEventInfo和newNotification方法被弃用,如何更新通知实现方式。通过Notification.Builder构建器创建通知,兼容Android8.0及更高版本。详细介绍了Notification.Builder的使用方法和各个参数的意义。

相关文章  通知的各种类型

Android6.0后setLatestEventInfo和new Notification()方法弃用,首先看一下setLatestEventInfo方法的使用:

Notification notification = new Notification(drawable,tickerText,System.currentTimeMillis()); //此处定义了一个Notification 
其中第一个参数代表图标第二个参数代表提示的内容,第三个参数是指要显示的时间,一般是当即显示,故填入系统当前时间。
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ActivityMain.class), 0);//该语句的作用是定义了一个不是当即显示的activity,只有当用户拉下notify显示列表,并且单击对应的项的时候,才会触发系统跳转到该activity.

notification.setLatestEventInfo(this, title, content, contentIntent);//在此处设置在notity列表里的该notifycation得显示情况。

替代方法:

notification = new Notification.Builder(context)
      .setChannelId(String.valueOf(Notification_ID))//该句适配android 8.0 版本
      .setSmallIcon(notifyIcon)
      .setContentTitle(notifyTitle)
      .setTicker(text)
      .setContentText(text)
      .setWhen(when)
      .setContentIntent(pi).setNumber(0)
      .getNotification();
notification.flags |= Notification.FLAG_AUTO_CANCEL;



点击显示通知按钮后, ic_notification图片没有出现任何现象 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp" tools:context=".MainActivity"> <!-- 顶部内容区域 --> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="#00ff00" android:textSize="16sp" android:text="This is TextView"/> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="false" android:text="Button"/> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:maxLines="1"/> <!-- 图片区域 --> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <ImageView android:id="@+id/imageView" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:scaleType="centerCrop" android:src="@drawable/bg_1"/> <ImageView android:id="@+id/imageView2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:scaleType="centerCrop" android:src="@drawable/ic_notification"/> </LinearLayout> <ProgressBar android:id="@+id/progressBar" android:layout_width="match_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:max="100"/> </LinearLayout> <!-- 服务按钮区域 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:id="@+id/startServiceBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start Service"/> <Button android:id="@+id/stopServiceBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stop Service"/> <Button android:id="@+id/bindServiceBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Bind Service"/> <Button android:id="@+id/unbindServiceBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="UnBind Service"/> </LinearLayout> <!-- 功能按钮区域 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="8dp"> <Button android:id="@+id/intent" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Music"/> <Button android:id="@+id/intent2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Camera"/> <Button android:id="@+id/intent3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Bluetooth"/> </LinearLayout> <!-- 底部按钮区域 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="8dp"> <Button android:id="@+id/wifi" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Wifi"/> <Button android:id="@+id/intent4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Network"/> <Button android:id="@+id/intent5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Database添加"/> <Button android:id="@+id/show_btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="显示通知"/> <Button android:id="@+id/cancel_btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消通知"/> </LinearLayout> </LinearLayout> 这是activity_main.xml页面
10-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值