刷新通知栏进度条

发布于 2024-08-30 00:44:05 字数 108 浏览 5 评论 0原文

我想在通知栏中放置一个进度条。这个想法是在程序将文件上传到服务器时显示进度条。其他一切都很好,但我不知道如何刷新通知内的进度条。有人知道有什么模式可以玩吗?我的意思是,我应该在服务或活动等中刷新进度条。

I would like to put a progress bar in the notification bar. The idea is showing the progress bar while the program uploads a file to a server. Everything else is ok, but I can not figure out how to refresh the progress bar inside the notification. Does anybody knows any pattern to play with? I mean, where I should refresh the progress bar, in a service or activity and so.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风蛊 2024-09-06 00:44:05

我不知道你的代码是什么样的,所以我不知道你需要修改什么,但我做了一些搜索文档。我在通知ProgressBarRemoteViews

具体来说,在RemoveView中,您可以更新进度栏。因此,结合每个链接中的一些示例代码,我得到如下内容:

public class MyActivity extends Activity {
    private static final int PROGRESS = 0x1;
    private static final int MAX_PROGRESS = 100;

    private int mProgressStatus = 0;

    private Handler mHandler = new Handler();

    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        //define Notification
        //...

        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
        contentView.setProgressBar(R.id.progress_bar, MAX_PROGRESS, mProgressStatus, false);
        notification.contentView = contentView;

        // Start file upload in a background thread
        new Thread(new Runnable() {
            public void run() {
                while (mProgressStatus < MAX_PROGRESS) {
                    mProgressStatus = doWork();

                    // Update the progress bar
                    mHandler.post(new Runnable() {
                        public void run() {
                            contentView.setProgressBar(R.id.progress_bar, MAX_PROGRESS, mProgressStatus, false);
                        }
                    });
                }
            }
        }).start();
    }
}

I don't know what your code looks like, so I don't know what you need to modify, butI did some searching through the documentation. I found some stuff on Notifications, ProgressBars, and RemoteViews.

Specifically, in RemoveView, you can update the Progress bar. So combining some of the example code in each link, I get something like this:

public class MyActivity extends Activity {
    private static final int PROGRESS = 0x1;
    private static final int MAX_PROGRESS = 100;

    private int mProgressStatus = 0;

    private Handler mHandler = new Handler();

    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        //define Notification
        //...

        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
        contentView.setProgressBar(R.id.progress_bar, MAX_PROGRESS, mProgressStatus, false);
        notification.contentView = contentView;

        // Start file upload in a background thread
        new Thread(new Runnable() {
            public void run() {
                while (mProgressStatus < MAX_PROGRESS) {
                    mProgressStatus = doWork();

                    // Update the progress bar
                    mHandler.post(new Runnable() {
                        public void run() {
                            contentView.setProgressBar(R.id.progress_bar, MAX_PROGRESS, mProgressStatus, false);
                        }
                    });
                }
            }
        }).start();
    }
}
乖乖 2024-09-06 00:44:05

要从 RemoteView 中删除 ProgressBar,请使用以下代码:-

 remoteViews.setViewVisibility(R.id.progressBar, View.INVISIBLE);

您也可以使用 View.GONE 但这将使 android 填充空白空间。

To remove a ProgressBar from RemoteView use the following code :-

 remoteViews.setViewVisibility(R.id.progressBar, View.INVISIBLE);

You can also use View.GONE but that will make android to fill empty space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文