双击退出和Notification通知

双击退出和Notification通知

  • 一,双击退出
    • activity中重写onKeyDown方法
  • 二,常用的通知
    • 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.普通通知

双击退出和Notification通知_第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();
                //发送对象   1.通知的id  2.通知对象
                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 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();
                //发送对象   1.通知的id  2.通知对象
                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 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();
                //发送对象   1.通知的id  2.通知对象
                manager.notify(1,build);

大图的通知双击退出和Notification通知_第2张图片

				//获取通知管理器
                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 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();
                //发送对象   1.通知的id  2.通知对象
                manager.notify(1,build);

列表项的通知

双击退出和Notification通知_第3张图片

				//获取通知管理器
                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 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();
                //发送对象   1.通知的id  2.通知对象
                manager.notify(1,build);

自定义的通知

双击退出和Notification通知_第4张图片

				//获取通知管理器
                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 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();
                //发送对象   1.通知的id  2.通知对象
                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());

进度条通知

双击退出和Notification通知_第5张图片

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);

你可能感兴趣的:(双击退出和Notification通知)