1.首先在main.xml文件中写入一些界面。
2. 定义一个progressBar 的布局文件
3. activity 中实现
public class main extends Activity {
/** Called when the activity is first created. */
int notification_id=19172439;
NotificationManager nm;
Handler handler=new Handler();
Notification notification;
int count=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bt1=(Button)findViewById(R.id.bt1);
bt1.setOnClickListener(bt1lis);
Button bt2=(Button)findViewById(R.id.bt2);
bt2.setOnClickListener(bt2lis);
//建立notification,前面有学习过,不解释了,不懂看搜索以前的文章
nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notification=new Notification(R.drawable.home,"图标边的文字",System.currentTimeMillis());
notification.contentView = new RemoteViews(getPackageName(),R.layout.notification);
//使用notification.xml文件作VIEW
notification.contentView.setProgressBar(R.id.pb, 100,0, false);
//设置进度条,最大值 为100,当前值为0,最后一个参数为true时显示条纹
//(就是在Android Market下载软件,点击下载但还没获取到目标大小时的状态)
Intent notificationIntent = new Intent(this,main.class);
PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
notification.contentIntent = contentIntent;
}
OnClickListener bt1lis=new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showNotification();//显示notification
handler.post(run);
}
};
Runnable run=new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
count++;
notification.contentView.setProgressBar(R.id.pb, 100,count, false);
//设置当前值为count
showNotification();//这里是更新notification,就是更新进度条
if(count<100) handler.postDelayed(run, 200);
//200毫秒count加1
}
};
OnClickListener bt2lis=new OnClickListener(){
@Override
public void onClick(View v) {
nm.cancel(notification_id);
//清除notification
}
};
public void showNotification(){
nm.notify(notification_id, notification);
}
}
个人觉得android 中消息栏中的进度条主要用到的是handler 控制进度,单纯的撇开项目来说没有什么大的意义,消息栏中的进度条主要是用在
一些下载,或者判断一些完成的任务中可以给用户一个很好的体验。