通知栏传值一直为第一次的传值的解决办法

PendingIntent contentItent = PendingIntent.getActivity(this, 0,

notificationIntent,0);

改为 PendingIntent contentItent = PendingIntent.getActivity(this, 0,

notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使Android Service一直存活并可以发送通知消息,可以使用前台服务。前台服务是一种在通知中显示通知的服务,该通知会告知用户该服务正在运行,并且即使应用程序处于后台或处于休眠状态,该服务也会保持运行状态。通过前台服务,您可以将服务与通知消息一起使用,以便用户能够知道该服务正在运行,并且可以随时访问该服务。 以下是实现前台服务的步骤: 1.创建一个继承自Service类的服务。 2.在服务中创建Notification对象并设置其标题、内容和图标。 3.将Notification对象传递给startForeground方法。 4.在服务的onDestroy方法中调用stopForeground方法,以便在服务停止时删除通知。 下面是一个示例代码: ``` public class MyService extends Service { private static final int NOTIFICATION_ID = 1; @Override public int onStartCommand(Intent intent, int flags, int startId) { // Create notification Notification notification = new NotificationCompat.Builder(this, "channel_id") .setContentTitle("My Service") .setContentText("Service is running") .setSmallIcon(R.drawable.ic_notification) .build(); // Start service in foreground startForeground(NOTIFICATION_ID, notification); // Do some work here return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); // Stop foreground and remove notification stopForeground(true); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } ``` 在这个示例中,我们创建了一个前台服务,并在通知中显示了一个通知,告知用户服务正在运行。当服务停止时,我们调用了stopForeground方法,以便删除通知
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值