android隐藏前台服务通知,如何在Android中更新前台服务的通知文本?

在Android中,要更新前台服务的通知,不需反复调用startForeground(),而是使用NotificationManager.notify()方法,确保使用相同的通知ID。这样,通知内容会更新而服务保持在前台状态。示例代码展示了如何创建和更新通知。
摘要由CSDN通过智能技术生成

如果要更新startForeground()设置的通知,只需构建新通知,然后使用NotificationManager通知它。

关键是使用相同的通知ID。

我没有测试反复调用startForeground()来更新Notification的场景,但我认为使用NotificationManager.notify会更好。

更新通知不会从前台状态中删除服务(这只能通过调用stopForground来完成);

例:

private static final int NOTIF_ID=1;

@Override

public void onCreate (){

this.startForeground();

}

private void startForeground() {

startForeground(NOTIF_ID, getMyActivityNotification(""));

}

private Notification getMyActivityNotification(String text){

// The PendingIntent to launch our activity if the user selects

// this notification

CharSequence title = getText(R.string.title_activity);

PendingIntent contentIntent = PendingIntent.getActivity(this,

0, new Intent(this, MyActivity.class), 0);

return new Noti

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值