Android 通知栏刷新进度条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2689729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh progress bar in notification bar
提问by ahmontero
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.
我想在通知栏中放一个进度条。这个想法是在程序将文件上传到服务器时显示进度条。其他一切正常,但我不知道如何刷新通知内的进度条。有人知道可以玩的任何模式吗?我的意思是,我应该在哪里刷新进度条,在服务或活动中等等。
采纳答案by seanmonstar
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.
我不知道你的代码是什么样的,所以我不知道你需要修改什么,但我在文档中做了一些搜索。我在Notifications、ProgressBars和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:
具体来说,在 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();
}
}
回答by Rorist
You can use custom views in Notification:
https://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView
您可以在通知中使用自定义视图:https:
//developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView
回答by Rohan Kandwal
To remove a ProgressBar from RemoteView use the following code :-
要从 RemoteView 中删除 ProgressBar,请使用以下代码:-
remoteViews.setViewVisibility(R.id.progressBar, View.INVISIBLE);
You can also use View.GONE
but that will make android to fill empty space.
您也可以使用,View.GONE
但这会使 android 填充空白空间。