一,双击退出
activity中重写onKeyDown方法
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
long secondTime = System.currentTimeMillis();
if (secondTime - firstTime > 2000) {
Toast.makeText(KeyEventActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
firstTime = secondTime;
return true;
} else{
finish();
}
}
return super.onKeyDown(keyCode, event);
}
二,常用的通知
通知
特殊 进度条 builder.setProgress(100,50,true);
自定义 builder.setContent(remoteViews); 1
.普通通知
//普通通知
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void normal_notification() {
//TODO 1:获得通知管理者:发送通知 取消通知
NotificationManager manager= (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
//TODO 2:创建构造者
Notification.Builder builder = new Notification.Builder(this);
//TODO 3:设置属性 setSamllIcon该属性必须设置
builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
builder.setContentTitle("我是标题");
builder.setContentText("我是内容");
//其他属性
builder.setTicker("我是提示信息");
builder.setContentInfo("我是附加信息");
//设置跳转其他页面
//1。创建意图对象
Intent intent= new Intent(this,OtherActivity.class);
//2.Intent对象转成PendingIntent
PendingIntent pendingIntent=PendingIntent.getActivity(this,100,intent,PendingIntent.FLAG_ONE_SHOT);
//3。设置跳转
builder.setContentIntent(pendingIntent);
//TODO 4:发送通知
//参数一 id 通知的id 参数二 通知对象
manager.notify(1,builder.build());
2.自定义通知
//自定义通知:builder.setContent(remoteViews);
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void customer_notification() {
//TODO 1:获得通知管理者:发送通知 取消通知
NotificationManager manager= (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
//TODO 2:创建构造者
Notification.Builder builder = new Notification.Builder(this);
//TODO 3:设置属性 setSamllIcon该属性必须设置
builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
builder.setContentTitle("我是标题");
builder.setContentText("我是内容");
//TODO :设置自定义布局:
RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.layout_customer_notification);
remoteViews.setTextViewText(R.id.customer_tv,"宋定行");//给文本设置文字
remoteViews.setImageViewResource(R.id.customer_image,R.drawable.ic_launcher_background);//给ImageView设置新的图片
builder.setContent(remoteViews);
//TODO 4:发送通知
//参数一 id 通知的id 参数二 通知对象
manager.notify(2,builder.build());
}
3.进度条通知
/进度条通知:setProgress
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void progress_notification() {
//TODO 1:获得通知管理者:发送通知 取消通知
final NotificationManager manager= (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
//TODO 2:创建构造者
final Notification.Builder builder = new Notification.Builder(this);
//TODO 3:设置属性 setSamllIcon该属性必须设置
builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
builder.setContentTitle("我是标题");
//TODO 设置进度条
//参数一 最大值 参数二:当前进度 参数三 是否模糊
// builder.setProgress(100,50,true);
final Timer timer=new Timer();
timer.schedule(new TimerTask() {
int progress;
@Override
public void run() {
//1.下载过程
builder.setContentText("正在下载,当前进度"+progress);
builder.setProgress(100,progress,false);//确定的进度条
progress+=10;
manager.notify(6,builder.build());
if(progress==100){
//2.安装过程
builder.setContentText("正在安装");
builder.setProgress(0,0,true);//安装模糊
manager.notify(6,builder.build());
try {
Thread.sleep(7000);//模拟安装过程
} catch (InterruptedException e) {
e.printStackTrace();
}
//3.安装完成
manager.cancel(6);//取消置顶的通知
timer.cancel();
handler.sendEmptyMessage(12);
}
}
}, 0, 1000);
}