通知是什么
分别有
一.普通通知
知识结构图
Notifcation的作用
Notifcation的6个方法
如何使用Notifcation,分为4大步骤
1.开始代码操作,
先在项目中创建按钮控件,在里面创建方法到MainActivity中
我们需要定义
//获取通知管理器
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
获取实例化通知对象,
Notification.Builder builder=new Notification.Builder(this); builder.setSmallIcon(R.mipmap.ic_launcher);//必须设置 builder.setContentTitle("普通通知"); builder.setContentText("我是普通通知");
假如你要点击通知,通知消失,并且退回页面,可以这样
紧接着在获取实例化通知对象后边添加
builder.setAutoCancel(true);//点击自动消失
这里还不能用,需要在添加Intent和PendingIntent
来实现
Intent intent=new Intent(this,MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this,101,intent,PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent);//跳转到指定的Activity
最后需要
Notification notification=builder.build(); //通知管理器发送通知 manager.notify(1,notification);
完整代码
效果
二 分组通知
知识结构图
创建一个控件,在里面定义一个方法
刚开始和普通差不多
//获取通知管理器
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
获取实例化通知对象,
Notification.Builder builder=new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);//必须设置
builder.setContentTitle("分组通知");
builder.setContentText("我是分组通知");
builder.setGroup("a");
和普通不同这里要加上
builder.setGroupSummary(true);//将这条通知折叠到最后一层
Notification.Builder builder1=new Notification.Builder(this);
builder1.setSmallIcon(R.mipmap.ic_launcher);//必须设置
builder1.setContentTitle("分组通知1");
builder1.setContentText("我是分组通知1");
builder1.setGroup("a");
Notification.Builder builder2=new Notification.Builder(this);
builder2.setSmallIcon(R.mipmap.ic_launcher);//必须设置
builder2.setContentTitle("分组通知2");
builder2.setContentText("我是分组通知2");
builder2.setGroup("a");
Notification notification = builder.build();
Notification notification1 = builder1.build();
Notification notification2 = builder2.build();
manager.notify(2,notification);
manager.notify(3,notification1);
manager.notify(4,notification2);
这里要弄3个才可以展示出2个,因为第一个要被占用
三。 进度条通知
知识结构图
如何使用带进度条的通知
思路
实际操作代码,创建控件,还是创建方法
这里要定义 ,因为关系到时间Timer timer;
刚开始还是老规矩
timer=new Timer()
NotificationManager manager
Notification.Builder builder=ne(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);//必须设置
builder.setContentTitle("正在下载...");
builder.setProgress(100,0,false);//最大值 当前进度 是否模糊显示
//通知管理器发送通知 manager.notify(5, builder.build());
调用timer
timer.schedule(new TimerTask() { int progress=0; @Override public void run() { if (progress==100){ manager.cancel(5);//取消通知---通知栏上通知消失 timer.cancel(); }else { progress+=20; builder.setProgress(100,progress,false); manager.notify(5,builder.build()); } } },0,1000);
整体代码
效果
四 自定义通知
知识结构图
创建控件
在layout定义类
代码,图片自己可以随便找个连接
效果
五 双击退出
利用线程延时实现
-
private static boolean mBackKeyPressed = false;//记录是否有首次按键
-
@Override
public void onBackPressed() {
if(!mBackKeyPressed){
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
mBackKeyPressed = true;
new Timer().schedule(new TimerTask() {//延时两秒,如果超出则擦错第一次按键记录
@Override
public void run() {
mBackKeyPressed = false;
}
}, 2000);
}
else{//退出程序
this.finish();
//System.exit(0);
}
} -
通过 mBackKeyPressed 来记录是否有首次按返回键的记录,如果不存在首次按键记录,则Toast提示,并记录首次按键记录,并启动线程在2秒后擦除该按键记录。如果在线程海内擦除mBackKeyPressed 时又按下返回键,则执行else里面的语句退出程序。