刷新在通知栏进度条 [英] Refresh progress bar in notification bar
问题描述
我想提出一个进度条,在通知栏。这个想法是显示进度条,而程序文件上载到服务器。一切是好的,但我想不出如何刷新通知里面的进度条。有谁知道任何图案一起玩?我的意思是,我应该在哪里刷新进度条,在服务或活动等。
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.
推荐答案
我不知道你的code的样子,所以我不知道你需要修改,布提做了一些搜索通过文档。我发现了一些东西在<一个href="http://developer.android.com/intl/de/guide/topics/ui/notifiers/notifications.html">Notifications, <一href="http://developer.android.com/intl/de/reference/android/widget/ProgressBar.html">ProgressBars,和<一href="http://developer.android.com/intl/de/reference/android/widget/RemoteViews.html">RemoteViews.
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.
具体而言,在RemoveView,您可以更新进度条。因此,结合一些例子code中的每一个环节,我得到的是这样的:
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();
}
}
这篇关于刷新在通知栏进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!