欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第三天

程序员文章站 2022-06-17 08:25:59
...

双击退出,Notitfcation 通知

1.双击退出

 /**
     * 该方法用来捕捉手机键盘被按下的事件。
     * @param keyCode  该参数指的是被按下的键的键盘码,手机键盘中每个按钮都会有其对应的键盘码,
     *                 在应用程序都是通过键盘码才知道用户按下的是哪个键。
     * @param event    当用户按下按键时,系统会自动将事件封装成KeyEvent对象供应用程序使用。
     *                 按键事件KeyEvent 按键事件有多种常量类型,比如 KEYCODE_BACK
     * @return
     */
     
    @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);
    }

Notification通知

1.普通通知

第三天
Java代码

/**
     * 普通通知
     * @param view
     */
    public void click(View view) {
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        //创建构造者
        Notification.Builder builder = new Notification.Builder(this);
        //设置属性   setSamllIcon该属性必须设置
        builder.setSmallIcon(R.mipmap.ic_launcher);//必须设置
        builder.setContentTitle("我是标题"); //建议设置
        builder.setContentText("我是内容");//建议设置
        //创建对象.发送的就是这个对象
        Notification build = builder.build();
        //TODO :发送通知
        //参数一 id 通知的id(稍后介绍意义)   参数二 通知对象
        notificationManager.notify(1,build);
    }

2.可点击的通知

/**
     * 可点击的通知
     * @param view
     */
    public void click2(View view) {
        //构造管理者
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentText("我是内容");
        builder.setContentTitle("我是标题");
        builder.setAutoCancel(true);
        //点击跳转本页面
        Intent intent = new Intent(this, MainActivity.class);
        /**
         * 可简单一个延时的intent 当点击通知栏的信息时,才发送intent
         * 第一个参数是上下文
         * 第二个参数是 请求码,多个请求码不一样即可
         * 第三个参数是 intent
         * 第四个参数是 flags 可写0;
         */
        PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        //        builder.setAutoCancel(true); //点击消息通知取消
        //设置意图对象
        builder.setContentIntent(activity);
        Notification build = builder.build();
        notificationManager.notify(1,build);
    }

3.可弹出的通知

第三天

 /**
     * 可弹出的通知
     * @param view
     */
    public void click3(View view) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentText("我是内容");
        builder.setContentTitle("我是标题");
        builder.setAutoCancel(true);
        builder.setPriority(Notification.PRIORITY_MAX);
        builder.setDefaults(Notification.DEFAULT_ALL);
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(activity);
        Notification build = builder.build();
        notificationManager.notify(1,build);
    }

4.大图通知

第三天
Java代码

/**
     * 大图通知
     * @param view
     */
    public void click4(View view) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("大图通知");
        builder.setSmallIcon(R.mipmap.ic_launcher);
        //通知内容为大图片
        Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
        bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
        builder.setStyle(bigPictureStyle);
        //不能跨APP
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent activity = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_ONE_SHOT);
        builder.setFullScreenIntent(activity,true);
        builder.setContentIntent(activity);
        notificationManager.notify(1,builder.build());
    }

5.列表通知

第三天
Java代码

/**
     * 列表通知
     * @param view
     */
    public void click5(View view) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("列表通知");
        //通知内容为列表显示
        Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
        inboxStyle.addLine("李白");
        inboxStyle.addLine("猴子");
        inboxStyle.addLine("石卓凡");
        builder.setStyle(inboxStyle);
        //不能跨APP
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent activity = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_ONE_SHOT);
        builder.setFullScreenIntent(activity,true);
        builder.setContentIntent(activity);
        notificationManager.notify(1,builder.build());
    }

6.自定义通知

第三天
Java代码

 /**
     * 自定义通知
     * @param view
     */
    public void click6(View view) {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentText("内容");
        builder.setContentTitle("头部");

        /**
         * RemoteViews是可以在别的进程(系统进程)中显示的View,并且提供了一组跨进程更新它界面的操作
         * 两个参数,第一个布局所在包名
         * 第二个是布局Id
         * 布局文件是自己创建的,随便一个线性布局,加一个textView和ImageView即可
         */

        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout);
        /**
         * 由于运行在不同的进程中,所以RemoteViews无法像正常的View一样更新UI。
         * RemoteViews提供了一系列的set方法,但是这些set方法只是View全部方法的子集。
         */

        //都是两个参数,第一个参数相当于findViewById,第二个是设置一个值.
        remoteViews.setTextViewText(R.id.textview,"啦啦啦,种太阳");
        remoteViews.setImageViewResource(R.id.imagebiew,R.mipmap.ic_launcher_name);

        builder.setCustomContentView(remoteViews);
        Notification build = builder.build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1,build);

    }

7.分组通知

第三天
Java代码

/**
     * 分组通知
     * @param view
     */
    public void click7(View view) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        Notification.Builder builder1 = new Notification.Builder(this);
        Notification.Builder builder2 = new Notification.Builder(this);
        Notification.Builder builder3 = new Notification.Builder(this);

        //消息分组属性,group相同才能分到一组
        builder.setGroup("2");
        builder.setSmallIcon(R.mipmap.ic_launcher_name);
        //被设置setGroupSummary为true的消息会隐藏内容,其内容会显示另外分组成员信息.
        builder.setGroupSummary(true);
        builder.setContentText("lalalalalala111");
        builder.setContentTitle("luelueluelue111");


        //消息分组属性,group相同才能分到一组
        builder1.setGroup("2");
        builder1.setSmallIcon(R.mipmap.ic_launcher_name);
        //被设置setGroupSummary为true的消息会隐藏内容,其内容会显示另外分组成员信息
//        builder1.setGroupSummary(true);
        builder1.setContentText("lalalalalala222");
        builder1.setContentTitle("luelueluelue222");

        builder2.setGroup("2");
        builder2.setSmallIcon(R.mipmap.ic_launcher_name);
//        builder2.setGroupSummary(true);
        builder2.setContentText("lalalalalala333");
        builder2.setContentTitle("luelueluelue333");

        builder3.setGroup("2");
        builder3.setSmallIcon(R.mipmap.ic_launcher_name);
//        builder3.setGroupSummary(true);
        builder3.setContentText("lalalalalala444");
        builder3.setContentTitle("luelueluelue444");

        notificationManager.notify(0,builder.build());
        notificationManager.notify(1,builder1.build());
        notificationManager.notify(2,builder2.build());
        notificationManager.notify(3,builder3.build());

    }

8.进度条通知

第三天
第三天
Java代码

/**
     * 进度条通知
     * @param view
     */
    public void click8(View view) {
        final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //TODO 2:创建构造者
        final Notification.Builder builder = new Notification.Builder(this);
        //TODO 3:设置属性   setSamllIcon该属性必须设置
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("我是标题");
        //TODO 设置进度条
        //参数一 最大值 参数二:当前进度 参数三 是否模糊
        //    builder.setProgress(100,50,true);
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int progress;
            @Override
            public void run() {
                //1.模拟下载过程
                builder.setContentText("正在下载,当前进度"+progress);
                builder.setProgress(100,progress,false).setColor(Color.BLUE);//确定的进度条
                progress+=10;
                notificationManager.notify(6,builder.build());
                if (progress==100){
                    //2.安装过程
                    builder.setContentText("正在安装");
                    builder.setProgress(0,0,true);//安装模糊
                    notificationManager.notify(6,builder.build());
                    try {
                        Thread.sleep(7000);//模拟安装过程
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //3.安装完成
                    notificationManager.cancel(6);//取消置顶的通知
                    timer.cancel();
                }
            }
        },0,1000);
    }