2019-08-05 Notification通知 双击退出

PendingIntent 说明

PendingIntent 是一种特殊的 Intent ,字面意思可以解释为延迟的 Intent ,用于在某个事件结束后执行特定的 Action 。从上面带 Action 的通知也能验证这一点,当用户点击通知时,才会执行。
PendingIntent 是 Android 系统管理并持有的用于描述和获取原始数据的对象的标志(引用)。也就是说,即便创建该PendingIntent对象的进程被杀死了,这个PendingItent对象在其他进程中还是可用的。
日常使用中的短信、闹钟等都用到了 PendingIntent

可点击通知+跳转

button2.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View view) {
                NotificationManager Manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification.Builder builder = new Notification.Builder(MainActivity.this);
                builder.setSmallIcon(R.mipmap.ic_launcher);
                builder.setContentText("内容");
                builder.setContentTitle("标题");

                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
                builder.setContentIntent(pendingIntent);
                Notification build = builder.build();
                Manager.notify(1,build);

            }
        });

普通通知

 //普通
        button.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View view) {
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification.Builder builder = new Notification.Builder(MainActivity.this);

                builder.setSmallIcon(R.mipmap.ic_launcher);
                builder.setContentTitle("标题啊");
                builder.setContentText("内容");

                builder.setTicker("提示信息");
                builder.setContentInfo("附加信息");

                //发送相同id就是更新
                Notification notification = builder.build();
                manager.notify(1,notification);
            }
        });

可弹出通知


//可弹出
        button4.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View view) {
                NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                Notification.Builder builder = new Notification.Builder(MainActivity.this);
                builder.setSmallIcon(R.mipmap.ic_launcher);
                builder.setContentTitle("我是标题");
                builder.setContentText("我是内容");
                builder.setAutoCancel(true);

                builder.setPriority(Notification.PRIORITY_MAX);
                builder.setDefaults(Notification.DEFAULT_ALL);

                builder.setTicker("我是提示信息");
                builder.setContentInfo("我是符加信息");

                Notification build = builder.build();
                manager.notify(6,build);
            }
        });

双击退出(1)

重写onKeyDown方法

 public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK && event.getAction()==KeyEvent.ACTION_DOWN){
            long millis = System.currentTimeMillis();
            if(millis-firstTime>2000){
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
                firstTime=millis;
                return true;
            }
            else {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

双击退出(2)

重写onBackPressed方法

public void onBackPressed() {
        long timeMillis = System.currentTimeMillis();
        if(timeMillis-firstTime>2000){
            Toast.makeText(this, "再按一次退出啊", Toast.LENGTH_SHORT).show();
            firstTime=timeMillis;
        }
        else{
            finish();
        }
    }

自定义通知

//自定义
        button3.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.N)
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View view) {
                //获得通知管理者:发送通知 取消通知
                NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                //创建构造者
                Notification.Builder builder = new Notification.Builder(MainActivity.this);
                //3:设置属性   setSamllIcon该属性必须设置
                builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
                builder.setContentTitle("标题");
                builder.setContentText("内容");
                //设置自定义布局:
                RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.mydiy);
                remoteViews.setTextViewText(R.id.diyTextView,"你不真正的快乐");//给文本设置文字
                remoteViews.setImageViewResource(R.id.diyImageView,R.mipmap.a);
                builder.setContent(remoteViews);
                //发送通知
                //参数一 id 通知的id   参数二 通知对象=
                manager.notify(9,builder.build());
            }
        });

进度条通知

//进度条通知
        button5.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View view) {
                final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                final Notification.Builder builder = new Notification.Builder(MainActivity.this);
                //设置属性   setSamllIcon该属性必须设置
                builder.setSmallIcon(R.mipmap.a);
                builder.setContentTitle("标题");
                //参数一 最大值 参数二:当前进度 参数三 是否模糊
//                    builder.setProgress(100,0,false);
                final Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    int progress;
                    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
                    @Override
                    public void run() {
                        builder.setContentText("正在下载"+progress);
                        builder.setProgress(100,progress,false);
                        progress+=10;
                        manager.notify(8,builder.build());
                        if(progress==100){
                            builder.setContentText("安装中..."+progress);
                            builder.setProgress(0,0,true);
                            manager.notify(8,builder.build());
                            try {
                                Thread.sleep(5000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            timer.cancel();
                            manager.cancel(8);
                        }
                    }
                },0,500);
                manager.notify(8,builder.build());
            }
        });

你可能感兴趣的:(2019-08-05 Notification通知 双击退出)