您的位置:首页 > 移动开发 > Android开发

在Android的Notification中显示进度条

2010-08-02 00:18 344 查看
转帖:http://www.abcfun.cn/n176c12.aspx



如果你在使用Android Market下载应用可能会发现StatusBar拉下后区域除了显示常规的图标、文字和描述外还有一个进度条指示。在Android的Notification中如何加入ProgressBar呢?我们发现NotificationManager类只涉及一个提示的显示和取消,相关的细节还是在构造Notification中实现,该类的contentView属性可以帮助我们制定一个RemoteViews的布局,通过setProgressBar来实现对RemoteViews的进度指示做刷新工作,如果你做过Android上的Widget,则不会对RemoteViews陌生吧,实现图片和文字的修改可以通过setImageViewResource或setTextViewText方法实现。Android开发网需要说明的是: RemoteViews在构造时第二个参数需要制定一个xml的布局文件。类似RemoteViews(getPackageName(),R.layout.android123); 而对于它的设置进度,4个参数中,第一个为RemoteViews中的ProcessBar ID,第二个参数为进度条的最大范围,第三个是当前的进度指示一般我们在单独的线程中处理逻辑,可以通过Handler实时回调显示状态比如setProgressBar(R.id.myProcessBar,maxProcess, currentPos, false);









转帖:http://www.abcfun.cn/n176c12.aspx

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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();
}
}

转帖:http://stackoverflow.com/questions/2689729/progress-bar-in-notification-bar

You can use custom views in Notification, http://developer.android.com/intl/fr/guide/topics/ui/notifiers/notifications.html#CustomExpandedView
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: