Notification Notification and Custom Toast

Day 4 Notification Notification and custom Toast
0, double-click the exit
a custom toast
two, commonly notification
Here Insert Picture Description
1. Ordinary notification
2. Custom notification
3. Progress notifications
III. 7.0 Andrews direct reply notification
IV. Notification packet
five the lock screen notification
0, double-click to exit the
activity overridden method 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);
    }

A custom toast

	//1.创建吐司对象
       Toast toast= new Toast(Main3Activity.this);
       View view2=LayoutInflater.from(Main3Activity.this).inflate(R.layout.pop,null);
        //2.设置自定义布局
       toast.setView(view2);
       //3.设置显示时长
       toast.setDuration(Toast.LENGTH_LONG);
       //4.显示吐司
       toast.show();

Second, the common notification
notification special
progress bar builder.setProgress (100,50, true);
custom builder.setContent (RemoteViews);
1. Common notifications

 //普通通知
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    private void normal_notification() {
        //TODO 1:获得通知管理者:发送通知 取消通知
        NotificationManager manager= (NotificationManager)
                getSystemService(Context.NOTIFICATION_SERVICE);
        //TODO 2:创建构造者
        Notification.Builder builder = new Notification.Builder(this);
        //TODO 3:设置属性   setSamllIcon该属性必须设置
        builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
        builder.setContentTitle("我是标题");
        builder.setContentText("我是内容");
        //其他属性
        builder.setTicker("我是提示信息");
        builder.setContentInfo("我是附加信息");
      
 
        //设置跳转其他页面
        //1。创建意图对象
        Intent intent= new Intent(this,OtherActivity.class);
        //2.Intent对象转成PendingIntent
        PendingIntent pendingIntent=PendingIntent.getActivity(this,100,intent,PendingIntent.FLAG_ONE_SHOT);
        //3。设置跳转
        builder.setContentIntent(pendingIntent);
        //TODO 4:发送通知
        //参数一 id 通知的id   参数二 通知对象
        manager.notify(1,builder.build());

2. Custom notification


   //自定义通知:builder.setContent(remoteViews);
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    private void customer_notification() {
        //TODO 1:获得通知管理者:发送通知 取消通知
        NotificationManager manager= (NotificationManager)
                getSystemService(Context.NOTIFICATION_SERVICE);
        //TODO 2:创建构造者
        Notification.Builder builder = new Notification.Builder(this);
        //TODO 3:设置属性   setSamllIcon该属性必须设置
        builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
        builder.setContentTitle("我是标题");
        builder.setContentText("我是内容");
        //TODO :设置自定义布局:
        RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.layout_customer_notification);
        remoteViews.setTextViewText(R.id.customer_tv,"宋定行");//给文本设置文字
        remoteViews.setImageViewResource(R.id.customer_image,R.drawable.ic_launcher_background);//给ImageView设置新的图片
        builder.setContent(remoteViews);
        //TODO 4:发送通知
        //参数一 id 通知的id   参数二 通知对象
        manager.notify(2,builder.build());

    } 

3. progress bar notification

   //进度条通知:setProgress
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void progress_notification() {
        //TODO 1:获得通知管理者:发送通知 取消通知
        final NotificationManager manager= (NotificationManager)
                getSystemService(Context.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);//确定的进度条
                progress+=10;
                manager.notify(6,builder.build());
                if(progress==100){
                    //2.安装过程
                    builder.setContentText("正在安装");
                    builder.setProgress(0,0,true);//安装模糊
                    manager.notify(6,builder.build());
                    try {
                        Thread.sleep(7000);//模拟安装过程
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    //3.安装完成
                    manager.cancel(6);//取消置顶的通知
                    timer.cancel();
                    handler.sendEmptyMessage(12);
                }
            }
        }, 0, 1000);

III. Android 7.0 respond directly notify
IV. Announcements Group
V. lock screen notification

Guess you like

Origin blog.csdn.net/lizhuang_666/article/details/91400266