Android通知栏与界面沟通

来源:互联网 发布:淘宝昆虫标本 编辑:程序博客网 时间:2024/05/18 14:22

Android通知栏与界面沟通

需求:

  1. 当收到新消息时,显示通知栏;
  2. 点击通知栏,无论在哪个页面,都需要跳转到消息界面;
  3. 当app在后台运行的时候也能收到通知,并且点击通知能够将app在前台显示,并跳转到消息界面;
  4. 当app退出过后也能收到消息;

难点:

  1. 没有在appApplication中定义ArrayList来装载已经启动的Activity;
  2. 消息界面不是一个activity,是一个fragment;
  3. 消息界面在MAinActivity中;
  4. 只要有消息更新,就要显示通知栏;
  5. app的重新启动,并跳转界面;
  6. 数据的刷新;

解决办法:

  1. 自定义一个activity(MineActivity),继承Activity,其余的activity继承MineActivity;
  2. 在MineActivity中定义一个(Receiver)广播接收者,并且在onCreate中注册,在onDestroy中解除注册。Receiver中只做一件事,将当前页面关闭(finish());
  3. 自定义一个(Server)服务,在里定义接受消息的方法,并且service使用startService启动,不使用stopService来停止service;
  4. 定义自己想要的通知栏:
//获取状态通知栏管理 NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  //实例化通知栏构造器NotificationCompat.Builder NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);  //对Builder进行配置 mBuilder.setContentTitle("测试标题")//设置通知栏标题      .setContentText("测试内容") /<span style="font-family: Arial;">/设置通知栏显示内容</span>      .setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) //设置通知栏点击意图  //  .setNumber(number) //设置通知集合的数量      .setTicker("测试通知来啦") //通知首次出现在通知栏,带上升动画效果的      .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间      .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级  //  .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消        .setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)      .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合      //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission     //实例化通知栏之后通过给他添加.flags属性赋值 Notification notification = mBuilder.build();  notification.flags = Notification.FLAG_AUTO_CANCEL;    //通过setContentIntent(PendingIntent intent)方法中的意图设置对应的flags  public PendingIntent getDefalutIntent(int flags){      //获取activities   PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags);     //获取Receiver   PendingIntent pendingIntent= PendingIntent.getBroadcast(this, 0, new Intent(), flags);     return pendingIntent;  } 
  1. 在Service中定义Receiver,用来接收通知栏的点击事件:
private BroadcastReceiver messageReceiver = new BroadcastReceiver() {        @Override        public void onReceive(Context context, Intent intent) {                /**                    SHOW    app在前台显示                    BACKGROUND     app在后台显示                    KILL       app被杀死了                */                int type = isBackground();//判断app的状态                if (type == SHOW) {                    //发送广播到MineActivity中,关闭activity                    、、、、、、                   //跳转到消息界面                   、、、、、、                } else if (type == BACKGROUND) {                  showApp();//切换app到前台                } else if (type == KILL) {                  startApp();//重启app                }            }        }
  1. 为了不让service停止运行,在onStartCommand方法中,将返回值设置为START_STICKY;
  2. 最后一步,在MainActivity中定义了Receiver,用来处理消息数据的刷新和fragment的切换;
0 0
原创粉丝点击