通知通知とカスタムトースト

4日目の通知の通知およびカスタムトースト
0、終了をダブルクリックして
カスタムトースト
2、一般的な通知
ここに画像を挿入説明
1.通常の通知
2.カスタム通知
3.進捗状況の通知
III。7.0アンドリュース直接返信通知
IV。通知パケットを
5ロック画面の通知
0、終了するにはダブルクリックして
活動オーバーライドされたメソッドを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.创建吐司对象
       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();

第二に、一般的な通知
通知特別な
プログレスバーbuilder.setProgress(100,50、真の);
カスタムbuilder.setContent(RemoteViews);
1.共通の通知

 //普通通知
    @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.カスタム通知


   //自定义通知: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.プログレスバーの通知

   //进度条通知: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。アンドロイド7.0応答に直接通知
IVを。アナウンスグループ
V.ロック画面通知

おすすめ

転載: blog.csdn.net/lizhuang_666/article/details/91400266