android 带ProgressBar的Notification通知栏

本文档展示了如何在Android中创建一个包含ProgressBar的Notification,用于显示进度。通过NotificationManager发出和取消通知,并通过Handler更新进度条。点击按钮启动和取消通知,线程模拟进度更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1  创建NotificationMananger 对象,负责发出和取消通知。
  NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

2 初始化Notification设置图片资源,显示的文本、时间、能否取消
Notification n = new Notification(R.drawable.chat, "Hello,there!" , System.currentTimeMillis());             

3 展示通知
nm.notify(0,n);

4 取消通知
        nm.cancelNotify(0,n);



public class MainActivity extends Activity implements OnClickListener {


private Notification notification;
private NotificationManager nManager;
public static int NOTIFICATION = 112;
private int progress =0;
private boolean running = true;
private Button bt_showNof,bt_cancelNof;

Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 1:
cancelNotification();
Toast.makeText(MainActivity.this, "通知消失", Toast.LENGTH_SHORT).show();
break;
case 2:
Log.e("test","测试数据:"+ progress);
notification.contentView.setProgressBar(R.id.ProgressBar01, 100, progress, false);
nManager.notify(NOTIFICATION, notification);
break;
default:
break;
}

}
};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initNotification();
bt_showNof = (Button) findViewById(R.id.bt_show);
bt_cancelNof = (Button) findViewById(R.id.bt_cancel);
bt_showNof.setOnClickListener(this);
bt_cancelNof.setOnClickListener(this);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


private void initNotification(){
nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification(R.drawable.ic_launcher, "正在同步...", System.currentTimeMillis());
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.contentView = new RemoteViews(this.getPackageName(), R.layout.notification);
notification.contentView.setProgressBar(R.id.ProgressBar01, 100, 0, false);
notification.contentIntent = PendingIntent.getActivity(this, 0, new Intent(this ,MainActivity.class), 0);

}

private void showNotification(){
running = true;
nManager.notify(NOTIFICATION, notification);
}

private void cancelNotification(){
if(nManager != null){
nManager.cancel(NOTIFICATION);
}
progress = 0;
running = false;
}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bt_show:
showNotification();
new MyThread().start();
break;
case R.id.bt_cancel:
cancelNotification();
default:
break;
}
}

class MyThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
while (running) {
progress = progress+10;
if(progress >= 110){
mHandler.sendEmptyMessage(1);
}else{
mHandler.sendEmptyMessage(2);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值