双击退出,Notitfcation 通知
双击退出
原理
实现的基本原理就是,当按下BACK键时,会被onKeyDown捕获,判断是BACK键,则执行exit方法。
判断用户两次按键的时间差是否在一个预期值之内,是的话直接直接退出,不是的话提示用户再按一次后退键退出。
Java代码
/**
* 该方法用来捕捉手机键盘被按下的事件。
* @param keyCode 该参数指的是被按下的键的键盘码,手机键盘中每个按钮都会有其对应的键盘码,
* 在应用程序都是通过键盘码才知道用户按下的是哪个键。
* @param event 当用户按下按键时,系统会自动将事件封装成KeyEvent对象供应用程序使用。
* 按键事件KeyEvent 按键事件有多种常量类型,比如 KEYCODE_BACK
* @return
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ){
//判断用户两次按键的时间差是否在一个预期值之内,是的话直接直接退出,不是的话提示用户再按一次后退键退出。
if(System.currentTimeMillis() - exitTime > 2000){
Toast.makeText(this,"在点就退出",Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
//当返回true时,表示已经完整地处理了这个事件,并不希望其他的回调方法再次进行处理,而当返回false时,
// 表示并没有完全处理完该事件,更希望其他回调方法继续对其进行处理,
return true;
}else{
finish(); //结束当前activity
}
}
return super.onKeyDown(keyCode, event);
}
Notifaction通知
定义:是在系统的通知栏中呈现多样式持久性消息的类
1、在通知栏显示
2、消息持久性
3、种类多样性
用处:
- 显示客户端的推送消息(如有新版本发布、广告、推荐新闻等)
- 显示正在进行的事物(如音乐播放器、版本更新时候的下载进度等)
- 显示接收到短消息,即时消息等信息(如QQ、微信、新浪、短信)
setSmallIcon() 与 setLargeIcon()
当 setSmallIcon() 与 setLargeIcon() 同时存在时, smallIcon 显示在通知的右下角, largeIcon 显示在左侧;当只设置 setSmallIcon() 时, smallIcon 显示在左侧。
普通通知
//创建构造者
Notification.Builder builder = new Notification.Builder(this);
//设置属性 setSamllIcon该属性必须设置
builder.setSmallIcon(R.mipmap.ic_launcher); //必须设置
builder.setContentTitle("我是标题"); //建议设置
builder.setContentText("我是内容"); //建议设置
// builder.setTicker("我是提示信息");
// builder.setContentInfo("我是附加信息"); //7.0以后已经过期
//创建对象.发送的就是这个对象
Notification build = builder.build();
//获取通知管理器,负责发通知、清除通知等
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//发送通知
//参数一 id 通知的id(稍后介绍意义) 参数二 通知对象
notificationManager.notify(1,build);
自定义通知
定义:
其实就是加载一个布局. 加载布局用到了 RemoteViews 代码中有详细注释.
RemoteViews设置的布局文件并不支持所有的View,以下是RemoteViews所支持的View:
布局
FrameLayout,LinearLayout,RelativeLayout,GridLayout
组件
Button,ImageView,ImageButton,TextView,ProgressBar,ListView,GridView,StackView,ViewStub,AdapterViewFlipper,ViewFlipper,AnalogClock,Chronometer
自定义布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:src="@mipmap/aaa"
/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
/>
</LinearLayout>
Java中的代码
// 创建构造者
Notification.Builder bd2 = new Notification.Builder(this);
// 必须添加小图片否则报错
bd2.setSmallIcon(R.mipmap.ic_launcher);
/**
* RemoteViews是可以在别的进程(系统进程)中显示的View,并且提供了一组跨进程更新它界面的操作
* 两个参数,第一个布局所在包名
* 第二个是布局Id
* 布局文件是自己创建的,随便一个线性布局,加一个textView和ImageView即可
*/
RemoteViews views = new RemoteViews(getPackageName(), R.layout.notify);
// 更改自定义布局中的数据
// views.setTextViewText(R.id.txt,"呕吼");
// views.setImageViewResource(R.id.img,R.mipmap.ic_launcher);
// 将自定义布局添加到通知构造者中
bd2.setCustomContentView(views);
Notification build2 = bd2.build();
manager.notify(2,build2);
进度条通知
详情请看Java代码,啥都有
final Notification.Builder bd3 = new Notification.Builder(this);
bd3.setSmallIcon(R.mipmap.ic_launcher_round);
bd3.setContentTitle("下载");
bd3.setContentText("正在下载...");
// 使用计时器设置进度条通知
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress = 0;
@Override
public void run() {
// 进度增幅
progress+=10;
// 设置进度,第一个参数是最大进度,第二个参数是当前进度,第三个参数为是否模糊
bd3.setProgress(100,progress,false);
// 刷新当前进度
manager.notify(3,bd3.build());
if (progress >= 100){
// 下载完成转成安装信息
bd3.setContentTitle("安装");
bd3.setContentText("安装中...");
bd3.setProgress(0,0,true);
manager.notify(3,bd3.build());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 安装完成,关闭通知
manager.cancel(3);
// 结束计时器
timer.cancel();
}
}
},0,1000);
break;
通知分组
详情请看Java代码,啥都有
Notification.Builder b1 = new Notification.Builder(this);
Notification.Builder b2 = new Notification.Builder(this);
Notification.Builder b3 = new Notification.Builder(this);
Notification.Builder b4 = new Notification.Builder(this);
b1.setSmallIcon(R.mipmap.ic_launcher_round);
b1.setContentTitle("one");
b1.setContentText("第一条");
// 设置分组Group,只有分到一个组,才能显示到一起
b1.setGroup("1");
// 设置GroupSummary属性,为true的时候会隐藏自身,来显示通知组中其他的通知
b1.setGroupSummary(true);
b2.setSmallIcon(R.mipmap.ic_launcher_round);
b2.setContentTitle("two");
b2.setContentText("第二条");
// 设置分组Group,只有分到一个组,才能显示到一起
b2.setGroup("1");
// 设置GroupSummary属性,为true的时候会隐藏自身,来显示通知组中其他的通知
// b2.setGroupSummary(true);
b3.setSmallIcon(R.mipmap.ic_launcher_round);
b3.setContentTitle("three");
b3.setContentText("第三条");
// 设置分组Group,只有分到一个组,才能显示到一起
b3.setGroup("1");
// 设置GroupSummary属性,为true的时候会隐藏自身,来显示通知组中其他的通知
// b3.setGroupSummary(true);
b4.setSmallIcon(R.mipmap.ic_launcher_round);
b4.setContentTitle("four");
b4.setContentText("第四条");
// 设置分组Group,只有分到一个组,才能显示到一起
b4.setGroup("1");
// 设置GroupSummary属性,为true的时候会隐藏自身,来显示通知组中其他的通知
// b4.setGroupSummary(true);
manager.notify(4,b1.build());
manager.notify(5,b2.build());
manager.notify(6,b3.build());
manager.notify(7,b4.build());
花式通知
Notification.Builder bd4 = new Notification.Builder(this);
bd4.setSmallIcon(R.mipmap.ic_launcher);
bd4.setContentTitle("爱情");
bd4.setContentText("好一个冠冕堂皇之词");
// 设置优先级 只有设置了优先级,才会自动弹出
bd4.setPriority(Notification.PRIORITY_MAX);
// 设置通知的各种属性,声音,震动,呼吸灯什么的
bd4.setDefaults(Notification.DEFAULT_ALL);
// 点击进行页面跳转
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
// 延时跳转PedingIntent(什么时候点,什么时候跳)
/**
* 可简单一个延时的intent 当点击通知栏的信息时,才发送intent
* 第一个参数是上下文
* 第二个参数是 请求码,多个请求码不一样即可
* 第三个参数是 intent
* 第四个参数是 flags 可写0;
*/
PendingIntent pendingIntent = PendingIntent.getActivity(this, 800, intent, PendingIntent.FLAG_UPDATE_CURRENT);
bd4.setContentIntent(pendingIntent);
// 跳转完页面自动删除通知
bd4.setAutoCancel(true);
// 添加列表的样式
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
// 添加一行的样式
inboxStyle.addLine("第一行");
inboxStyle.addLine("第二行");
inboxStyle.addLine("第三行");
inboxStyle.addLine("第四行");
inboxStyle.addLine("第五行");
bd4.setStyle(inboxStyle);
// 添加大图
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.aaa));
bd4.setStyle(bigPictureStyle);
manager.notify(8,bd4.build());
PendingIntent
说明
PendingIntent 是一种特殊的 Intent ,字面意思可以解释为延迟的 Intent ,用于在某个事件结束后执行特定的 Action 。从上面带 Action 的通知也能验证这一点,当用户点击通知时,才会执行。
PendingIntent 是 Android 系统管理并持有的用于描述和获取原始数据的对象的标志(引用)。也就是说,即便创建该PendingIntent对象的进程被杀死了,这个PendingItent对象在其他进程中还是可用的。
日常使用中的短信、闹钟等都用到了 PendingIntent
构造方法说明
/获取一个用于启动 Activity 的 PendingIntent 对象
public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags);
//获取一个用于启动 Service 的 PendingIntent 对象
public static PendingIntent getService(Context context, int requestCode, Intent intent, int flags);
//获取一个用于向 BroadcastReceiver 广播的 PendingIntent 对象
public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags)
PendingIntent 具有以下几种 flag:
FLAG_CANCEL_CURRENT:
如果当前系统中已经存在一个相同的 PendingIntent 对象,那么就将先将已有的 PendingIntent 取消,然后重新生成一个 PendingIntent 对象。
FLAG_NO_CREATE:
如果当前系统中不存在相同的 PendingIntent 对象,系统将不会创建该 PendingIntent 对象而是直接返回 null 。
FLAG_ONE_SHOT:
该 PendingIntent 只作用一次。
FLAG_UPDATE_CURRENT:
如果系统中已存在该 PendingIntent 对象,那么系统将保留该 PendingIntent 对象,但是会使用新的 Intent 来更新之前 PendingIntent 中的 Intent 对象数据,例如更新 Intent 中的 Extras 。