第一行代码-8.1 使用通知

来源:互联网 发布:nginx 支持ipv6 编辑:程序博客网 时间:2024/05/21 09:57

1、通知的基本用法
  先看看怎么创建一个通知事件。

// NotificationTest-MainActivity.javaprivate Button mBSendNotify;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    mBSendNotify = (Button) findViewById(R.id.send_notice);    mBSendNotify.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View arg0) {            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);            // Notification notification = new Notification(R.drawable.ic_launcher, "这是通知", System.currentTimeMillis());            // notification.setLatestEventInfo(this, "这是通知的标题", "这是通知的内容", null); // 这个方法已经过时,不能使用            Notification.Builder builder = new Notification.Builder(MainActivity.this);            builder.setContentTitle("这是通知标题");            builder.setContentText("这是通知内容");            builder.setSmallIcon(R.drawable.ic_launcher);            Notification notification = builder.getNotification();                manager.notify(1, notification);        }    });}


  现在实现的通知点击之后没有效果,需要通过PendingIntent来实现。与Intent不同的是Intent 更加倾向于去立即执行某个动作,而PendingIntent 更加倾向于在某个合适的时机去执行某个动作。所以,也可以把PendingIntent 简单地理解为延迟执行的Intent。
  怎么获取PendingIntent呢?根据需求有几种方式:getActivity、getBroadcast和getService,三种方法的参数都一样:第一个参数依旧是Context,不用多做解释。第二个参数一般用不到,通常都是传入0 即可。第三个参数是一个Intent 对象,我们可以通过这个对象构建出PendingIntent 的“意图”。第四个参数用于确定PendingIntent 的行为,有FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT 和FLAG_UPDATE_CURRENT 这四种值可选。这四种参数到底有什么不同呢?
  FLAG_ONE_SHOT:表示PendingIntent的事件只会执行一次,比如跳转活动(后面也会说明),第一次点击通知,会跳转到另一个活动,第二次点击的时候就不会了。
  FLAG_UPDATE_CURRENT:最常用的模式,当pendingIntent更新的时候,如果第二个参数(requestCode)的值相同,就会更新前面插入的pendingIntent的intent(同时notification里面的pendingIntent也会更新)。如果不相同,pendingIntent也会更新,但是之前插入到notification里面的pendingIntent不会更新。
  FLAG_NO_CREATE:和前一个方法相反,如果第二个参数相同,pendingIntent的参数就保持原来插入的值,不会因为intent的不同而改变。
  FLAG_CANCEL_CURRENT:暂时还没找到例子。

// MainActivity.java-添加的代码Intent intent = new Intent(MainActivity.this, NotificationActivity.class);                intent.putExtra("name", "通知");                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);                builder.setContentIntent(pendingIntent);

  但是可以发现点击了通知之后,虽然活动跳转了,但是通知还在。怎么把通知去除呢?只要在另一个活动中把上一个通知取消掉就可以了。注意id要匹配。

// NotificationActivity.java@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_notification);    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);    manager.cancel(1); // 与notify的id相同}

2、通知的高级技巧
  通过上一节的学习我们知道了如何发送通知,但这个通知还没有任何提示效果,比如发声、震动、闪烁等。这一节我们就要学习怎么实现这些效果。
(1)sound属性
  notification有一个属性名为sound,它是uri类型的,只要把它指向手机中的音乐,就可以在通知显示的时候播放这段音乐。比如,现在手机上的/system/media/audio/alarms目录下有一个Water.ogg音频文件,则可以这么写:

Uri soundUri = Uri.fromFile(new File("/system/media/audio/alarms/Water.ogg"));notification.sound = soundUri;

(2)vibrate属性
  这个属性实现的是震动效果,它的类型是长整形数组,表示时间,以毫秒为单位。数组中第一个元素的值表示刚发送通知时,手机静止的时间,第二个元素值表示接下来手机震动的时间,第三个元素又表示手机静止的时间,以此类推。那么我要实现手机先静止1秒,然后震动一秒,再静止一秒,最后震动一秒,怎么办?

long[] vibrate = new long[] {0, 1000, 1000, 1000};notification.vibrate = vibrate; // 设置震动效果

  震动还需要设置权限:

<uses-permission android:name="android.permission.VIBRATE"/>

(3)LED
  现在的手机基本上都会前置一个LED 灯,当有未接电话或未读短信,而此时手机又处于锁屏状态时,LED 灯就会不停地闪烁,提醒用户去查看。我们可以使用ledARGB、ledOnMS、ledOffMS 以及flags 这几个属性来实现这种效果。ledARGB 用于控制LED 灯的颜色,一般有红绿蓝三种颜色可选。ledOnMS 用于指定LED 灯亮起的时长,以毫秒为单位。ledOffMS用于指定LED 灯暗去的时长,也是以毫秒为单位。flags 可用于指定通知的一些行为,其中就包括显示LED 灯这一选项。所以,当通知到来时,如果想要实现LED 灯以绿色的灯光一闪一闪的效果,就可以写成:

notification.ledARGB = Color.GREEN;notification.ledOnMS = 1000;notification.ledOffMS = 1000;notification.flags = Notification.FLAG_SHOW_LIGHTS;

  不过如果你不想这么麻烦设置这些效果的话,也可以使用默认的效果,会自动根据手机的设置来设置铃声、震动和LED。

notification.defaults = Notification.DEFAULT_ALL;

  我的手机的效果就是和短信来的效果一样。
  下面是一些参考网址:
http://my.oschina.net/youranhongcha/blog/196933
http://www.cnblogs.com/fengzhblog/archive/2013/07/21/3203363.html
http://zhiweiofli.iteye.com/blog/1972513

0 0
原创粉丝点击