我的专高一—Day03(Notification通知)

 

目录

双击退出

定义

效果图片

代码文件

Notification通知

普通通知

定义

为什么使用Notitfcation?

Notifcation的常用方法及底层实现

Notification普通通知的基本使用步骤

通知分组

步骤

锁屏通知

进度条通知

步骤

自定义通知

代码文件

效果

核心代码

结束


定义

实现的基本原理就是,当按下BACK键时,会被onKeyDown捕获,判断是BACK键,则执行exit方法。
判断用户两次按键的时间差是否在一个预期值之内,是的话直接直接退出,不是的话提示用户再按一次后退键退出。

效果图片

代码文件

/**
     * 该方法用来捕捉手机键盘被按下的事件。
     * @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通知

普通通知

定义

是在系统的通知栏中呈多样式持久消息的类。

为什么使用Notitfcation?

为了能够在系统的通知栏中显示系统或程序发出的消息,Android引用Notification

Notifcation的常用方法及底层实现

setContentitle():设置标题

setContenText():设置内容

setWhen():设置通知时间

setSmallcon():设置通知的小图标(必须要有不然软件会报错)

setLargelcon():设置通知的大图标

setAutoCancel():自动删除通知

Notification普通通知的基本使用步骤

01:获取通知管理器(招聘领导)

NotificationManager  nm= getSystemService(NOTIFICATION_SERVICE);

底层实现:

参数介绍:NOTIFICATION_SERVICE:获取系统通知的参数

02:实例化通知对象(招募员工)

Notification.Builder notificationBuilder= new Notification.Builder(this);

03:设置通知的属性(员工信息)

notificationBuilder.setSmallIcon(R.mipmap.image);

notificationBuilder.setContentTitle("10086");

notificationBuilder.setContentText("充值成功,余额30.00元!");  

notificationBuilder.setAutoCancel(true);

04:通知管理器发送通知(领导命令员工)

显示通知

   Notification  notification= notificationBuilder.build();  

   nm.notify( 1,notification);

 


private void sendNotification() {
        //创建构造者
        Notification.Builder builder = new Notification.Builder(this);
        //设置属性   setSamllIcon该属性必须设置
        builder.setSmallIcon(R.mipmap.ic_launcher); //必须设置
        builder.setContentTitle("我是标题"); //建议设置
        builder.setContentText("我是内容"); //建议设置
//        builder.setTicker("我是提示信息");
//        builder.setContentInfo("我是附加信息"); //7.0以后已经过期
        //创建对象.发送的就是这个对象
        Notification build = builder.build();
        //获取通知管理器,负责发通知、清除通知等
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //TODO :发送通知
        //参数一 id 通知的id(稍后介绍意义)   参数二 通知对象
        notificationManager.notify(1,build);
    }
}

通知分组

步骤

1、获取通知管理器

NotificationManager mNotifacationManager = getSystemService(NOTIFICATION_SERVICE);

2、实例化多个通知

NotificationCompat.Builder mBuilder1 = new NotificationCompat.Builder(this);

NotificationCompat.Builder mBuilder2 = new NotificationCompat.Builder(this);

3、设置通知的属性(通知分组)

mBuilder1.setWhen(System.currentTimeMillis())

 .setContentTitle("first content title")          

.setContentText("first content text")        

 .setSmallIcon(R.mipmap.ic_launcher)        

 .setGroup("a");

mBuilder1.setWhen(System.currentTimeMillis())  

      ...          

.setGroup("b");

4、发送通知

mNotifacationManager.notify(1, mBuilder1.build());

mNotifacationManager.notify(2, mBuilder2.build());

锁屏通知

build的时候调用 setVisibility() 方法设置即可 例:.setVisibility(VISIBILITY_PUBLIC).build();

进度条通知

步骤

1、获取通知管理器

NotificationManager nm= getSystemService(NOTIFICATION_SERVICE);

2、实例化通知

 NotificationCompat.Builder builder= new NotificationCompat.Builder(this);

 builder.setContentText("安装中...");

 builder.setProgress(0,0,true); 

3、设置通知的属性(进度条属性)

4、发送通知

new Thread(new Runnable() {

public void run() {

for(int i=0;i<100;i++){  

builder.setProgress(100,i,false);

nm.notify(3,builder.build());

}                  

自定义通知

代码文件

private void userNotification() {
        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.simple_layout);
          
}                 

效果

核心代码

private void notificationStyle() {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("列表通知");
//        builder.setContentTitle("大图通知");

        //通知内容为大图片
        Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
        bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
 //通知内容为列表显示
        Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
        inboxStyle.addLine("李白");
        inboxStyle.addLine("猴子");
        inboxStyle.addLine("露娜");
        builder.setStyle(inboxStyle);
        //不能跨APP
        Intent intent = new Intent(this, MainActivity.class);
        //intent - PendingIntent
        PendingIntent intent1 = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_ONE_SHOT);
        builder.setFullScreenIntent(intent1, true);
        builder.setContentIntent(intent1);
        manager.notify(9, builder.build());

结束

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值