Android广播机制使用了观察着模式;

(1) 通知

1) 获取状态通知栏管理

NotificationManager 是一个系统Service,所以必须通过getSystemService(NOTIFICATION_SERVICE)方法来获取。

notificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);

2) 实例化通知栏构造器NotificationCompat.Builder

Notification.Builder notificationBuilder = new Notification.Builder($.getContext())
        .setSmallIcon(R.drawable.notification_app_icon) // 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap
        
.setPriority(Notification.PRIORITY_MAX)
        .setTicker(message)// 设置在status
        
.setContentTitle(title)// 设置在下拉status
        
.setContentText($.util.strCut(message, 100))
        .setNumber(1); // TextView的右方显示的数字,可放大图片看,在最右侧。这个number同时也起到一个序列号的左右,如果多个触发多个通知(同一ID),可以指定显示哪一个。

3) 设置PendingIntent

Intent intent = new Intent($.getContext(), LaunchActivity.class);
intent.putExtra(LaunchActivity.BUNDLE_EXTRA_NOTIFICATION, bundle);
PendingIntent pendingIntent2 = PendingIntent.getActivity($.getContext(), 0,
        intent, PendingIntent.FLAG_CANCEL_CURRENT);
notificationBuilder.setContentIntent(pendingIntent2);

这里PendingIntent表示的是就是启动APP,而FLAG_CANCEL_CURRENT 表示相应的PendingIntent已经存在,则取消前者,然后创建新的PendingIntent

4) 设置铃声和震动执行一次

Notification notification =builder.build();

notification.flags =Notification.FLAG_ONLY_ALERT_ONCE;

这里Notification.FLAG_ONLY_ALERT_ONCE就是表示的铃声和震动只执行一次。

更多相关文章

  1. Android之selector
  2. android Gridview生成程序快捷键的复杂方法
  3. 图片背景平铺
  4. android 控件状态配置文件
  5. Android(安卓)几种图像特效处理的集锦
  6. Android中几种图片特效的处理的实现方法
  7. Android中实现输入图片地址浏览图片操作
  8. Android通过Movie展示Gif格式图片
  9. android 设置开机不自动进入锁屏状态

随机推荐

  1. Android的发展历史
  2. 做Android的感想
  3. android 仿微信demo————微信通讯录界
  4. android:layout_gravity 和 android:grav
  5. Android:res之selector背景选择器
  6. android layout attribute
  7. android:layout_gravity和android:gravit
  8. android 圆角编写(懒得去找,写给自己看的)
  9. android系统定制从听说到入门四
  10. android:inputType参数类型说明