20

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.

4

3 回答 3

17

我不知道你的代码是什么样的,所以我不知道你需要修改什么,但我通过文档进行了一些搜索。我在NotificationsProgressBarsRemoteViews上找到了一些东西。

具体来说,在 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();
    }
}
于 2010-04-27T21:19:10.190 回答
1

您可以在通知中使用自定义视图:
https ://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView

于 2010-04-27T20:58:00.177 回答
0

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

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

您也可以使用View.GONE,但这会使 android 填补空白。

于 2013-12-25T11:12:33.753 回答