双击退出和Notification通知
- 一,双击退出
- 二,常用的通知
- 1.普通通知
- 可点击的通知
- 带提示音的通知
- 大图的通知![在这里插入图片描述](https://img-blog.csdnimg.cn/20190805210938204.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0pvaG5fQW5kZXJzb24=,size_16,color_FFFFFF,t_70)
- 列表项的通知
- 自定义的通知
- 带分组的通知
- 进度条通知
一,双击退出
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);
}
二,常用的通知
1.普通通知
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(Main2Activity.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("这是标题");
builder.setContentText("这是通知内容");
Notification build = builder.build();
manager.notify(1,build);
可点击的通知
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(Main2Activity.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("这是标题");
builder.setContentText("这是通知内容");
Intent intent = new Intent(Main2Activity.this, Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(Main2Activity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
Notification build = builder.build();
manager.notify(1,build);
带提示音的通知
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(Main2Activity.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("这是标题");
builder.setContentText("这是通知内容");
Intent intent = new Intent(Main2Activity.this, Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(Main2Activity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setPriority(Notification.PRIORITY_MAX);
builder.setDefaults(Notification.DEFAULT_ALL);
Notification build = builder.build();
manager.notify(1,build);
大图的通知
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(Main2Activity.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("这是标题");
builder.setContentText("这是通知内容");
Intent intent = new Intent(Main2Activity.this, Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(Main2Activity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setStyle(bigPictureStyle);
builder.setPriority(Notification.PRIORITY_MAX);
builder.setDefaults(Notification.DEFAULT_ALL);
Notification build = builder.build();
manager.notify(1,build);
列表项的通知
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(Main2Activity.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("这是标题");
builder.setContentText("这是通知内容");
Intent intent = new Intent(Main2Activity.this, Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(Main2Activity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setPriority(Notification.PRIORITY_MAX);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_ALL);
Notification.InboxStyle inboxStyle = new Notification.InboxStyle()
.addLine("111")
.addLine("222")
.addLine("333");
builder.setStyle(inboxStyle);
Notification build = builder.build();
manager.notify(1,build);
自定义的通知
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(Main2Activity.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("这是标题");
builder.setContentText("这是通知内容");
Intent intent = new Intent(Main2Activity.this, Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(Main2Activity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setPriority(Notification.PRIORITY_MAX);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_ALL);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.remote_layout);
remoteViews.setImageViewResource(R.id.iv_remote,R.mipmap.ic_launcher_round);
remoteViews.setTextViewText(R.id.tv_remote,"这是信息内容");
builder.setCustomContentView(remoteViews);
Notification build = builder.build();
manager.notify(1,build);
自定义布局
<?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/iv_remote"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/tv_remote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"/>
</LinearLayout>
带分组的通知
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder1 = new Notification.Builder(Main2Activity.this);
Notification.Builder builder2 = new Notification.Builder(Main2Activity.this);
Notification.Builder builder3 = new Notification.Builder(Main2Activity.this);
Notification.Builder builder4 = new Notification.Builder(Main2Activity.this);
builder1.setGroup("1");
builder1.setGroupSummary(false);
builder1.setSmallIcon(R.mipmap.ic_launcher);
builder1.setContentTitle("111");
builder1.setContentText("111");
builder2.setGroup("1");
builder2.setGroupSummary(false);
builder2.setSmallIcon(R.mipmap.ic_launcher);
builder2.setContentTitle("222");
builder2.setContentText("222");
builder3.setGroup("1");
builder3.setGroupSummary(false);
builder3.setSmallIcon(R.mipmap.ic_launcher);
builder3.setContentTitle("333");
builder3.setContentText("333");
builder4.setGroup("1");
builder4.setGroupSummary(false);
builder4.setSmallIcon(R.mipmap.ic_launcher);
builder4.setContentTitle("444");
builder4.setContentText("444");
manager.notify(7,builder1.build());
manager.notify(8,builder2.build());
manager.notify(9,builder3.build());
manager.notify(10,builder4.build());
进度条通知
final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
final Notification.Builder builder = new Notification.Builder(Main2Activity.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("这是标题");
final Handler handler = new Handler();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress;
@Override
public void run() {
if(progress==100){
builder.setContentText("正在安装");
builder.setProgress(0,0,true);
manager.notify(8,builder.build());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(Main2Activity.this, "下载完成", Toast.LENGTH_SHORT).show();
}
});
manager.cancel(8);
timer.cancel();
}
builder.setContentText("当前下载进度:"+progress);
progress+=10;
builder.setProgress(100,progress,false);
manager.notify(8,builder.build());
}
},0,1000);