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

有帮助吗?

解决方案

我不知道您的代码是什么样子,所以我不知道您需要修改什么,但是我在文档中进行了一些搜索。我在上面找到了一些东西 通知, 进度栏, , 和 远程视图.

具体来说,在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();
    }
}

其他提示

要从远程视图中删除progressbar,请使用以下代码: -

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top