其他分享
首页 > 其他分享> > 第四天

第四天

作者:互联网

通知和Fragment

模拟安装

 //管理者
                final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                final Notification.Builder builder = new Notification.Builder(MainActivity.this);
                builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
                builder.setContentTitle("我是标题");

                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(1, builder.build());
                        if (progress == 100) {
                            //2.安装过程
                            builder.setContentText("正在安装");
                            builder.setProgress(0, 0, true);//安装模糊
                            manager.notify(1, builder.build());
                            try {
                                Thread.sleep(3000);//模拟安装过程
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            //3.安装完成
                            manager.cancel(1);//取消置顶的通知
                            timer.cancel();//结束timer
                        }
                    }
                }, 0, 1000);

通知跳转页面

PendingIntent 具有以下几种 flag:
FLAG_CANCEL_CURRENT:
如果当前系统中已经存在一个相同的 PendingIntent 对象,那么就将先将已有的 PendingIntent 取消,然后重新生成一个 PendingIntent 对象。

FLAG_NO_CREATE:
如果当前系统中不存在相同的 PendingIntent 对象,系统将不会创建该 PendingIntent 对象而是直接返回 null 。

FLAG_ONE_SHOT:
该 PendingIntent 只作用一次。

FLAG_UPDATE_CURRENT:
如果系统中已存在该 PendingIntent 对象,那么系统将保留该 PendingIntent 对象,但是会使用新的 Intent 来更新之前 PendingIntent 中的 Intent 对象数据,例如更新 Intent 中的 Extras 。

  NotificationManager systemService = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification.Builder builder = new Notification.Builder(Tong_zhi_tiao_zhuan.this);
                builder.setContentText("内容");
                builder.setContentTitle("标题");
                builder.setSmallIcon(R.mipmap.ic_launcher);
                Intent intent = new Intent(Tong_zhi_tiao_zhuan.this, MainActivity.class);
                /**
                 * 可简单一个延时的intent 当点击通知栏的信息时,才发送intent
                 * 第一个参数是上下文
                 * 第二个参数是 请求码,多个请求码不一样即可
                 * 第三个参数是 intent
                 * 第四个参数是 flags 可写0;
                 */
                PendingIntent activity = PendingIntent.getActivity(Tong_zhi_tiao_zhuan.this, 888, intent, PendingIntent.FLAG_CANCEL_CURRENT);
                builder.setContentIntent(activity);
                builder.setAutoCancel(true);
                builder.setPriority(Notification.PRIORITY_MAX);
                builder.setDefaults(Notification.DEFAULT_ALL);
                systemService.notify(1, builder.build());

自定义通知

NotificationManager systemService = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification.Builder builder = new Notification.Builder(Tong_zhi_tiao_zhuan.this);
                builder.setContentText("内容");
                builder.setContentTitle("标题");
                builder.setSmallIcon(R.mipmap.ic_launcher);
  //自定义布局
   /**
                 * 第一个参数: 包名
                 * 第二个参数: 布局名称
                 */
                RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.zidingyi_layout);
                builder.setCustomContentView(remoteViews);
                //更改内容
                remoteViews.setTextViewText(R.id.txt, "内容2");
                systemService.notify(1, builder.build());

fragment

点击右键创建fragment

动态创建

main.activity

<FrameLayout
        android:id="@+id/ff"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
//d动态加载
        //管理器
        FragmentManager manager = getSupportFragmentManager();
        //开启事务
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        //创建的fragment
        BlankFragment blankFragment = new BlankFragment();
        //添加碎片
        fragmentTransaction.add(R.id.ff, blankFragment);
        //提交
        fragmentTransaction.commit();

静态加载

<fragment
<!--id必须加-->
         android:id="@+id/f"
         <!--name是全路径名 copy reference-->
         android:name="com.example.fragment_.BlankFragment"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
weixin_46361828 发布了4 篇原创文章 · 获赞 0 · 访问量 51 私信 关注

标签:NotificationManager,Notification,builder,manager,第四天,new,PendingIntent
来源: https://blog.csdn.net/weixin_46361828/article/details/104482531