Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以在服务中使用startForeground(id,notification)来正确更新通知进度条吗?_Java_Android_Android Service_Android Notifications_Notificationmanager - Fatal编程技术网

Java 我可以在服务中使用startForeground(id,notification)来正确更新通知进度条吗?

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

在这种情况下,我需要在几毫秒后更新通知进度条。如果我使用通知管理器更新进度,那么即使在调用stopForground(true)之后,通知也会显示,这不是我所需要的

下面是使用通知管理器进行更新的示例代码

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你能解释一下你提供的链接如何帮助回答我的问题吗?