JAVA命令符找不到符号_java: 找不到符号 符号: 方法 setLatestEventInfoentInfo

Error:(38, 25) 错误: 找不到符号

符号: 方法 setLatestEventInfo(MyService,String,String,PendingIntent)

位置: 类型为Notification的变量 notification

解决:

如果编译的SDK版本在API23以上,就会出现这个问题,因为setLatestEventInfo方法在api23中被删掉了

在api4以上的版本用notification可以用support v4中的NotificationCompat.Builder。android文档中给了一个例子:

Notification noti = new Notification.Builder(mContext)

.setContentTitle("New mail from " + sender.toString())

.setContentText(subject)

.setSmallIcon(R.drawable.new_mail)

.setLargeIcon(aBitmap)

.build();

把上面"Notification.Builder"替换成"NotificationCompat.Builder"

alter+enter 把supportv4中的相关的库import进来

import android.support.v4.app.NotificationCompat;

于是代码编程了这样

Notification notification;

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {

notification = new NotificationCompat.Builder(this)

.setSmallIcon(R.mipmap.ic_launcher)//必须要先setSmallIcon,否则会显示默认的通知,不显示自定义通知

.setTicker("显示于屏幕顶端状态栏的文本")

.setContentTitle("this ics content title")

.setContentText("this is content text")

.setContentIntent(pendingIntent)

.build();

} else {

notification = new Notification.Builder(this)

.setSmallIcon(R.mipmap.ic_launcher)//必须要先setSmallIcon,否则会显示默认的通知,不显示自定义通知

.setTicker("显示于屏幕顶端状态栏的文本")

.setContentTitle("this is content title")

.setContentText("this is content text")

.setContentIntent(pendingIntent)

.build();

}

startForeground(2,notification);

注意:!!!上面这段代码是前台服务的notification,通过startForeground让服务变成前台服务并显示通知。这时必须要setSmallIcon,否则会显示默认的通知,不显示自定义通知!!!如果是正常的用NotificationManager显示通知则无所谓顺序

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值