双击back键退出与Notitfcation
双击back退出
@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);
}
Notitfcation的用法
Notitfcation是创建手机信息提示框的方法
常用方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建构造者
Notification.Builder builder = new Notification.Builder(MainActivity.this);
//设置属性 setSamllIcon该属性必须设置
builder.setSmallIcon(R.mipmap.ic_launcher); //必须设置
builder.setContentTitle("我是标题"); //建议设置
builder.setContentText("我是内容"); //建议设置
//创建对象.发送的就是这个对象
Notification build = builder.build();
//获取通知管理器,负责发通知、清除通知等
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//发送通知
//参数一 id 通知的id 参数二 通知对象
notificationManager.notify(1,build);
}
});
}
进度条
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Notification.Builder builder = new Notification.Builder(Main2Activity.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentText("内容");
builder.setContentTitle("标题");
//创建进度条
builder.setProgress(100,0,false);
final Notification build = builder.build();
final NotificationManager systemService = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
systemService.notify(1,build);
//创建timer实现模拟更新进度
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress=0;
@Override
public void run() {
builder.setContentText("正在下载...");
builder.setContentTitle("下载");
progress+=10;
builder.setProgress(100,progress,false);
systemService.notify(1,build);
if (progress==100){
builder.setContentText("正在安装...");
builder.setContentTitle("下载");
systemService.notify(1,build);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
systemService.cancel(1);
timer.cancel();
}
}
},0,1000);
}
});
点击通知传跳
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationManager systemService = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(Main3Activity.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentText("内");
builder.setContentTitle("asdf");
//优先值
builder.setPriority(Notification.PRIORITY_MAX);
//设置通知灯呼吸,震动等效果
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
bigPictureStyle.bigLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
builder.setStyle(bigPictureStyle);
//点击转跳
Intent intent = new Intent(Main3Activity.this, Main2Activity.class);
//监听事件
PendingIntent activity = PendingIntent.getActivity(Main3Activity.this, 800, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(activity);
systemService.notify(1,builder.build());
}
});