Android用户界面之notifaction(状态栏通知)

对于通知,应先明白:通知概要 通知标题 通知内容 通知图标 通知时间

首先,看界面。状态栏上的是:图标和概要。

将状态栏往下拉,会出来标题、内容和时间。

通知除了状态栏的图标外,还可以 开打设备上的LED灯,发送声音、震动来提醒用户

通知可以告诉用户在后台发生了某事,所以经常在广播接受者和服务中使用;

例子开始的界面如下:点击发送后,会发送通知。

主要用到的方法有:

1、得到通知管理者:通过getSystemService(String).来得到NotificationManager,在该类中调用cancel(int)来清除通知。

2、指定通知的最新信息:notification.setLatestEventInfo(this, title, content, pendingIntent);

3、 PendingIntent类解析:

带有特定标记(flag)的的intent,由静态方法getActivity(Context, int, Intent, int flag),getBroadcast(Context, int, Intent, int flag),getService(Context, int, Intent, int flag)来创建。

主要标记有:

FLAG_UPDATE_CURRENT :如果已经存在PendingIntent,还产生该PendingIntent,还带有新的extra

FLAG_ONE_SHOT :这个PendingIntent只能被用一次。

FLAG_CANCEL_CURRENT:如果存在的PendingIntent还未消失,还取消将将要产生的该PendingIntent

主要代码:

[java] view plain copy
  1. StringtickerText=shortText.getText().toString();
  2. Stringtitle=titleText.getText().toString();
  3. Stringcontent=contentText.getText().toString();
  4. //1、得到NotificationManager
  5. NotificationManagermanager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  6. //2、实例化一个通知,指定了图标、概要、时间
  7. Notificationnotification=newNotification(android.R.drawable.stat_notify_chat,tickerText,System.currentTimeMillis());
  8. //3、指定通知的标题、内容和intent
  9. Intentintent=newIntent(Intent.ACTION_CALL,Uri.parse("tel:104040444"));
  10. PendingIntentpendingIntent=PendingIntent.getActivity(this,0,intent,0);
  11. notification.setLatestEventInfo(this,title,content,pendingIntent);
  12. //指定标志和声音
  13. notification.flags=Notification.FLAG_AUTO_CANCEL;
  14. notification.defaults=Notification.DEFAULT_SOUND;
  15. //可以指定为震动,也可以用.sound来自己指定声音 (sound是一个Uri对象.如何指定Raw文件可以见博文:http://blog.csdn.net/java2009cgh/article/details/7549679)
  16. //notification.defaults=Notification.DEFAULT_VIBRATE;
  17. //可以指定为闪光灯
  18. //notification.defaults=Notification.DEFAULT_LIGHTS;
  19. //4、发送通知给通知管理者
  20. manager.notify(1,notification);

转:http://blog.csdn.net/kuangc2008/article/details/6359110


更多相关文章

  1. Android实现https网络通信之添加指定信任证书/信任所有证书
  2. 通过wifi与设备进行通信(Android)
  3. [置顶] Android(安卓)通知栏Notification的整合 全面学习 (一个DE
  4. android 手动分包,将指定class打包到主dex中
  5. 关于Android(安卓)7.0系统通知声音不能播放
  6. android开发之Launcher icon(启动图标)的设计
  7. [置顶] 【通知】▁▂▃ Himi 著作《Android游戏编程之从零开始》
  8. application中 android:icon 和 android:roundIcon 的区别
  9. 【Bugly干货分享】关于 Android(安卓)N 那些你不知道的事儿

随机推荐

  1. Android 布局(LinearLayout\TableLayout
  2. app在android 6.0或以上平台版本运行过程
  3. adb,logcat使用及对Android设备的操作
  4. 如何监听Phone的状态,第三方App如何拨打/
  5. android 自定义带进度值的圆形进度条
  6. Android BottomNavigationBar底部导航控
  7. 下拉刷新系列二:SmartRefreshLayout 使用
  8. Annotation Processor在Android下的应用
  9. Android网络编程之——Android登录系统模
  10. Android媒体扫描代码分析