2
A
回答
2
您可以通过以下方式执行 使用进度栏的关键是使用“线程”运行时间消耗任务,并使用另一个“线程”相应地更新进度栏状态。阅读代码的评论,它应该是不言自明的。
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MyAndroidAppActivity extends Activity {
Button btnStartProgress;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
private long fileSize = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
btnStartProgress = (Button) findViewById(R.id.btnStartProgress);
btnStartProgress.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// prepare for a progress bar dialog
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("File downloading ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
// reset progress bar status
progressBarStatus = 0;
// reset filesize
fileSize = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
// process some tasks
progressBarStatus = doSomeTasks();
// your computer is too fast, sleep 1 second
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
// ok, file is downloaded,
if (progressBarStatus >= 100) {
// sleep 2 seconds, so that you can see the 100%
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// close the progress bar dialog
progressBar.dismiss();
}
}
}).start();
}
});
}
// file download simulator... a really simple
public int doSomeTasks() {
while (fileSize <= 1000000) {
fileSize++;
if (fileSize == 100000) {
return 10;
} else if (fileSize == 200000) {
return 20;
} else if (fileSize == 300000) {
return 30;
}
// ...add your own
}
return 100;
}
}
打开“布局/ main.xml中”文件,只需添加普通按钮进行演示。
<Button
android:id="@+id/btnStartProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Download File" />
1
要显示通知:
NotificationManager notificationManager = (NotificationManager) mContext
.getSystemService(NOTIFICATION_SERVICE);
Notification updateComplete = new Notification();
updateComplete.icon = android.R.drawable.stat_notify_sync;
updateComplete.tickerText = mContext
.getText(R.string.app_name);
updateComplete.when = System.currentTimeMillis();
Intent notificationIntent = new Intent(mContext,
MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0,
notificationIntent, 0);
String contentTitle = mContext.getText(R.string.app_name)
.toString();
String contentText;
contentText = mContext.getText(
R.string.yes).toString();
updateComplete.setLatestEventInfo(mContext, contentTitle,
contentText, contentIntent);
notificationManager.notify(progressBarStatus, updateComplete);
了解更多信息:
http://mobile.tutsplus.com/tutorials/android/android-fundamentals-status-bar-notifications/
相关问题
- 1. 如何在通知区域显示进度条?
- 2. 通知区域的进度条未关闭,未启动第二个进度条
- 3. 从AsynTask更新下载进度条到通知区域
- 4. 显示进度条直到下一个活动被加载
- 5. 进度条加载活动或加载按钮进度
- 6. 隐藏通知区域中的进度条时,100%完成
- 7. 点击通知区域没有显示消息在活动
- 8. 如何在通知区域后面显示Android活动
- 9. 下载管理器进度在通知区域中不可见
- 10. 加载时显示进度条
- 11. 显示进度条在加载列表
- 12. 数据加载时显示进度条
- 13. 显示下载进度条
- 14. 在Android中加载另一个活动时无法显示进度条
- 15. 通知中的进度条
- 16. 显示进度对话框,直到新的活动加载
- 17. AJAX进度条显示页面加载的进度百分比
- 18. 如何在显示活动前显示进度条
- 19. 进度条(循环)显示虽然显示活动
- 20. 通知显示,但不加载下一个活动
- 21. 在通知区域显示一个winform
- 22. 显示进度条动画
- 23. JTable加载值的变化,加载时显示进度条
- 24. 在通知中显示进度
- 25. 在$ http post中显示进度通知?
- 26. 显示区域的CSS滚动条
- 27. 如何在科尔多瓦的通知区域显示通知?
- 28. C2DM在gmail的通知区域显示通知
- 29. 在通知栏和活动中的多个下载显示
- 30. 在科尔多瓦本地通知中显示进度条
谢谢;我需要把进度条放在通知区域(状态栏) – Saeed
欢迎,然后你可以使用这段代码来显示。 –
您是否计划在通知区域显示progressbar,而没有progrssDialog? – 2012-11-17 07:11:45