技术小白,把学到的觉得有用的东西放在上面,希望不要见笑。如果需要,欢迎转载。
带有进度条的顶部通知
声明全局变量
private PendingIntent pendingIntent;
private NotificationManager notificationManager = null;
private NotificationCompat.Builder builder;
在使用的地方实例化
notificationManager = (NotificationManager) .getSystemService(Context.NOTIFICATION_SERVICE);
builder = new NotificationCompat.Builder(this);
Intent intent = new Intent(this, NetActivity.class);
pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
设置顶部通知的样式
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("下载顶部通知");
builder.setContentText("带有进度条的顶部通知,增加用户体验度");
程序演示,模拟进度条进度
new Thread(new Runnable(){
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
builder.setProgress(100, i, false);
notificationManager.notify(13, builder.build());
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
builder.setContentText("下载完毕");
notificationManager.notify(14, builder.build());
notificationManager.cancel(13);
}
}).start();