Java 我可以在服务中使用startForeground(id,notification)来正确更新通知进度条吗?
在这种情况下,我需要在几毫秒后更新通知进度条。如果我使用通知管理器更新进度,那么即使在调用stopForground(true)之后,通知也会显示,这不是我所需要的 下面是使用通知管理器进行更新的示例代码Java 我可以在服务中使用startForeground(id,notification)来正确更新通知进度条吗?,java,android,android-service,android-notifications,notificationmanager,Java,Android,Android Service,Android Notifications,Notificationmanager,在这种情况下,我需要在几毫秒后更新通知进度条。如果我使用通知管理器更新进度,那么即使在调用stopForground(true)之后,通知也会显示,这不是我所需要的 下面是使用通知管理器进行更新的示例代码 private void doStuff() { // starting in foreground startForeground(123, notification.build()); final Thread thread = new Thread(new R
private void doStuff() {
// starting in foreground
startForeground(123, notification.build());
final Thread thread = new Thread(new Runnable() {
@Override
public void run() {
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
timer++;
if(timer > 10){
stopForeground(true);
stopSelf();
return;
}
if(!bound){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
notificationManager.notify(123, notification.setProgress(10, timer, false).build());
}
}, 2000);
}
handler.postDelayed(this, 1000);
}
}, 1000);
}
});
thread.start();
}
否则,如果我使用startForground()方法,则通知在2000毫秒后不会显示,我将获得所需的行为,如下所示:
private void doStuff() {
// starting in foreground
startForeground(123, notification.build());
final Thread thread = new Thread(new Runnable() {
@Override
public void run() {
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
timer++;
if(timer > 10){
stopForeground(true);
stopSelf();
return;
}
if(!bound){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startForeground(123, notification.setProgress(10, timer, false).build());
}
}, 2000);
}
handler.postDelayed(this, 1000);
}
}, 1000);
}
});
thread.start();
}
我的问题是,我不知道这样做是否会出什么问题,因为我从未见过有人这样做。请帮忙!谢谢 @GhasemSadeghi你能解释一下你提供的链接如何帮助回答我的问题吗?@GhasemSadeghi你能解释一下你提供的链接如何帮助回答我的问题吗?