双击退出、Notitfcation通知
双击退出
Notitfcation通知
自定义通知
进度条通知
分组通知
交互式通知
双击退出
public class MainActivity extends AppCompatActivity {
private long extiTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK){
if (System.currentTimeMillis() - extiTime >=2000){
Toast.makeText(this, "你再点我,我就生气咯", Toast.LENGTH_SHORT).show();
extiTime = System.currentTimeMillis();
return true;
}else {
finish();
Toast.makeText(this, "你个大坏蛋!!!", Toast.LENGTH_SHORT).show();
}
}
return super.onKeyDown(keyCode, event);
}
}
Notitfcation通知
private void semd() {
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("表头");
builder.setContentText("你是啊哈");
Timer timer = new Timer();
Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,notification);
自定义通知
private void semd() {
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("表头");
builder.setContentText("你是啊哈");
Timer timer = new Timer();
Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,notification);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.mydecide);
remoteViews.setTextViewText(R.id.text,"你是一个傻子");
remoteViews.setImageViewResource(R.id.imageview,R.mipmap.ic_launcher);
builder.setCustomContentView(remoteViews);
Notification build = builder.build();
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(2,build);
进度条通知
private void progress() {
final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
final Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher_round);
builder.setContentTitle("我是标题");
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress;
@Override
public void run() {
builder.setContentText("正在下载,当前进度"+progress);
builder.setProgress(100,progress,false);
progress+=10;
manager.notify(6,builder.build());
if (progress==100){
builder.setContentText("正在安装");
builder.setProgress(0,0,true);
manager.notify(6,builder.build());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
manager.cancel(6);
timer.cancel();;
}
}
},0,1000);
}
分组通知
private void group() {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder1 = new Notification.Builder(this);
Notification.Builder builder2 = new Notification.Builder(this);
Notification.Builder builder3 = new Notification.Builder(this);
Notification.Builder builder4 = new Notification.Builder(this);
Notification.Builder builder5 = new Notification.Builder(this);
builder1.setGroup("2");
builder1.setSmallIcon(R.mipmap.ic_launcher);
builder1.setContentText("000000");
builder1.setContentTitle("111111");
builder2.setGroup("2");
builder2.setSmallIcon(R.mipmap.ic_launcher);
builder2.setContentText("222222");
builder2.setContentTitle("333333");
builder2.setGroupSummary(true);
builder3.setGroup("2");
builder3.setSmallIcon(R.mipmap.ic_launcher);
builder3.setContentText("444444");
builder3.setContentTitle("555555");
builder4.setGroup("2");
builder4.setSmallIcon(R.mipmap.ic_launcher);
builder4.setContentText("666666");
builder4.setContentTitle("7777777");
builder5.setGroup("2");
builder5.setSmallIcon(R.mipmap.ic_launcher);
builder5.setContentText("888888");
builder5.setContentTitle("999999");
manager.notify(1,builder1.build());
manager.notify(2,builder2.build());
manager.notify(3,builder3.build());
manager.notify(4,builder4.build());
}
交互式通知
private void jiaohu() {
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("傻子");
builder.setContentText("笨蛋");
builder.setPriority(Notification.PRIORITY_MAX);
builder.setDefaults(Notification.DEFAULT_ALL);
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
inboxStyle.addLine("傻瓜");
inboxStyle.addLine("傻蛋");
inboxStyle.addLine("傻子");
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
builder.setStyle(bigPictureStyle);
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,notification);
}