转至:http://blog.csdn.net/wdaming1986/article/details/7017742

  • 下面来谈谈notification,这个notification一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个快讯,这时手从上方滑动状态栏就可以展开并处理这个快讯。发现这个功能特别好用,所以我就根据我的理解来谈谈。摘自帮助文档 : notification类表示一个持久通知将提交给用户使用NotificationManager。已添加Notification.Builder,使其更容易构建通知。notification是一种让你的应用程序在没有开启情况下或在后台运行警示用户。它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径。

先来区分以下状态栏状态条的区别:

1、状态条就是手机屏幕最上方的一个条形状的区域;

在状态条有好多信息量:比如usb连接图标,手机信号图标,电池电量图标,时间图标等等;

2、状态栏就是手从状态条滑下来的可以伸缩的view;

在状态栏中一般有两类(使用FLAG_标记):

(1)正在进行的程序;

(2)是通知事件;

大概来描述创建一个Notification传送的信息有:

1、一个状态条图标;

2在拉伸的状态栏窗口中显示带有大标题,小标题,图标的信息,并且有处理该点击事件:比如调用该程序的入口类;

3、闪光,LED,或者震动;

快速创建一个Notification的步骤简单可以分为以下四步:

第一步:通过getSystemService()方法得到NotificationManager对象;

第二步:对Notification的一些属性进行设置比如:内容,图标,标题,相应notification的动作进行处理等等;

第三步:通过NotificationManager对象的notify()方法来执行一个notification的快讯;

第四步:通过NotificationManager对象的cancel()方法来取消一个notificatioin的快讯;

下面对Notification类中的一些常量,字段,方法简单介绍一下:

常量:

DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等

DEFAULT_LIGHTS 使用默认闪光提示

DEFAULT_SOUNDS 使用默认提示声音

DEFAULT_VIBRATE使用默认手机震动

说明】:加入手机震动,一定要在manifest.xml中加入权限

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

以上的效果常量可以叠加,即通过

mNotifaction.defaults =DEFAULT_SOUND| DEFAULT_VIBRATE;

或mNotifaction.defaults |=DEFAULT_SOUND (最好在真机上测试,震动效果模拟器上没有)

//设置flag位

FLAG_AUTO_CANCEL该通知能被状态栏的清除按钮给清除掉

FLAG_NO_CLEAR该通知能被状态栏的清除按钮给清除掉

FLAG_ONGOING_EVENT通知放置在正在运行

FLAG_INSISTENT是否一直进行,比如音乐一直播放,知道用户响应

常用字段:

contentIntent 设置PendingIntent对象,点击时发送该Intent

defaults 添加默认效果

flags设置flag位,例如FLAG_NO_CLEAR等

icon设置图标

sound设置声音

tickerText显示在状态栏中的文字

when 发送此通知的时间戳

Notification.build构造Notification方法介绍:

void setLatestEventInfo(Context context , CharSequencecontentTitle,CharSequencecontentText,PendingIntent contentIntent)

功能: 显示在拉伸状态栏中的Notification属性,点击后将发送PendingIntent对象

参数: context上下文环境

contentTitle 状态栏中的大标题

contentText 状态栏中的小标题

contentIntent 点击后将发送PendingIntent对象

说明:要是在Notification中加入图标,在状态栏和状态条中显示图标一定要用这个方法,否则报错!

最后说一下NotificationManager类的常用方法:

通过获取系统服务来获取该对象:

NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE) ;

NotificationManager常用方法介绍:

public void cancelAll() 移除所有通知(只是针对当前Context下的Notification)

public void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)

public void notify(String tag ,int id, Notification notification)将通知加入状态栏, 标签为tag,标记为id

public void notify(int id, Notification notification)将通知加入状态栏,,标记为id

下面看一下demo的效果图:

图(1) 图(2)

图(3) 图(4)

图(5)

源码奉上:

在NotificationApp工程里面:

一、在com.cn.notification.daming包下面NotificationMainActivity.java中的代码:

[java] view plain copy print ?
  1. packagecom.cn.notification.daming;
  2. importandroid.app.Activity;
  3. importandroid.app.Notification;
  4. importandroid.app.NotificationManager;
  5. importandroid.app.PendingIntent;
  6. importandroid.content.Intent;
  7. importandroid.content.SharedPreferences;
  8. importandroid.media.RingtoneManager;
  9. importandroid.net.Uri;
  10. importandroid.os.Bundle;
  11. importandroid.preference.PreferenceManager;
  12. importandroid.view.View;
  13. importandroid.view.View.OnClickListener;
  14. importandroid.widget.Button;
  15. publicclassNotificationMainActivityextendsActivityimplementsOnClickListener{
  16. privateButtonsetNotificationSoundBtn=null;
  17. privateButtonshowNotificatioBtn=null;
  18. privateButtoncancelNotificationBtn=null;
  19. privateIntentmIntent=null;
  20. privatePendingIntentmPendingIntent=null;
  21. privateNotificationmNotification=null;
  22. privateNotificationManagermNotificationManager=null;
  23. privatestaticfinalintNOTIFICATION_STATE=1;
  24. privatestaticfinalintRINGTONE_PICKED=2;
  25. privateUrinotifiSounds=null;
  26. @Override
  27. publicvoidonCreate(BundlesavedInstanceState){
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.main);
  30. mNotificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  31. setNotificationSoundBtn=(Button)findViewById(R.id.button0);
  32. setNotificationSoundBtn.setOnClickListener(this);
  33. showNotificatioBtn=(Button)findViewById(R.id.button1);
  34. showNotificatioBtn.setOnClickListener(this);
  35. cancelNotificationBtn=(Button)findViewById(R.id.button2);
  36. cancelNotificationBtn.setOnClickListener(this);
  37. mIntent=newIntent(this,ToNotificationActivity.class);
  38. mPendingIntent=PendingIntent.getActivity(this,0,mIntent,0);
  39. mNotification=newNotification();
  40. }
  41. publicvoidonClick(Viewv){
  42. //TODOAuto-generatedmethodstub
  43. switch(v.getId()){
  44. caseR.id.button0:
  45. SharedPreferencessharedPreferences=PreferenceManager.getDefaultSharedPreferences(this);
  46. Intentintent=newIntent(RingtoneManager.ACTION_RINGTONE_PICKER);
  47. //Allowusertopick'Default'
  48. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT,true);
  49. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI,RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
  50. //Showonlyringtones
  51. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_NOTIFICATION);
  52. //Don'tshow'Silent'
  53. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT,false);
  54. Stringnotifi_sound=sharedPreferences.getString("notification_sounds",null);
  55. if(notifi_sound!=null){
  56. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,Uri.parse(notifi_sound));
  57. }
  58. //Launch!
  59. startActivityForResult(intent,RINGTONE_PICKED);
  60. break;
  61. caseR.id.button1:
  62. mNotification.icon=R.drawable.daming;
  63. mNotification.tickerText="大明ZeroSonNotification";
  64. mNotification.sound=notifiSounds;
  65. mNotification.defaults=Notification.DEFAULT_VIBRATE|Notification.DEFAULT_LIGHTS;
  66. mNotification.flags=Notification.FLAG_INSISTENT;
  67. mNotification.setLatestEventInfo(this,"大明Notification","ThisisDaming`sNotificationTest!",mPendingIntent);
  68. mNotificationManager.notify(NOTIFICATION_STATE,mNotification);
  69. break;
  70. caseR.id.button2:
  71. mNotificationManager.cancel(NOTIFICATION_STATE);
  72. break;
  73. default:break;
  74. }
  75. }
  76. @Override
  77. protectedvoidonResume(){
  78. super.onResume();
  79. }
  80. @Override
  81. protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
  82. SharedPreferencessharedPreferences=PreferenceManager.getDefaultSharedPreferences(this);
  83. SharedPreferences.Editoreditor=sharedPreferences.edit();
  84. if(resultCode!=RESULT_OK){
  85. return;
  86. }
  87. switch(requestCode){
  88. caseRINGTONE_PICKED:{
  89. notifiSounds=data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
  90. editor.putString("notification_sounds",notifiSounds.toString());
  91. editor.commit();
  92. break;
  93. }
  94. default:break;
  95. }
  96. }
  97. }

二、在com.cn.notification.daming包下面ToNotificationActivity.java中的代码:

[java] view plain copy print ?
  1. packagecom.cn.notification.daming;
  2. importandroid.app.Activity;
  3. importandroid.content.SharedPreferences;
  4. importandroid.media.Ringtone;
  5. importandroid.media.RingtoneManager;
  6. importandroid.net.Uri;
  7. importandroid.os.Bundle;
  8. importandroid.preference.PreferenceManager;
  9. importandroid.widget.TextView;
  10. publicclassToNotificationActivityextendsActivity{
  11. privateTextViewtextview=null;
  12. @Override
  13. protectedvoidonCreate(BundlesavedInstanceState){
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main1);
  16. SharedPreferencessharedPreferences=PreferenceManager.getDefaultSharedPreferences(this);
  17. textview=(TextView)findViewById(R.id.textview);
  18. Stringnotificationsound=sharedPreferences.getString("notification_sounds",null);
  19. if(notificationsound==null){
  20. textview.setText("默认Notification声音");
  21. }else{
  22. Ringtoneringtone=RingtoneManager.getRingtone(ToNotificationActivity.this,Uri.parse(notificationsound));
  23. Stringtitle=ringtone.getTitle(ToNotificationActivity.this);
  24. textview.setText(title);
  25. }
  26. }
  27. }

三、在layout下main.xml布局文件的代码

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. android:gravity="center"
  12. android:layout_marginBottom="10dip"
  13. />
  14. <Button
  15. android:id="@+id/button0"
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content"
  18. android:text="设置Notification的sounds"
  19. android:layout_marginBottom="10dip"
  20. />
  21. <Button
  22. android:id="@+id/button1"
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content"
  25. android:text="发送Notification"
  26. android:layout_marginBottom="10dip"
  27. />
  28. <Button
  29. android:id="@+id/button2"
  30. android:layout_width="fill_parent"
  31. android:layout_height="wrap_content"
  32. android:text="取消Notification"
  33. android:layout_marginBottom="10dip"
  34. />
  35. </LinearLayout>

四、在layout下main1.xml布局文件的代码

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:gravity="center"
  11. android:textSize="10pt"
  12. android:text="大明原创"
  13. android:layout_marginTop="10dip"
  14. android:layout_marginBottom="10dip"
  15. />
  16. <TextView
  17. android:id="@+id/textview"
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:gravity="center"
  21. android:textSize="10pt"
  22. android:layout_marginTop="10dip"
  23. android:layout_marginBottom="10dip"
  24. />
  25. </LinearLayout>

五、manifest.xml中的代码:

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.cn.notification.daming"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdkandroid:minSdkVersion="8"/>
  7. <uses-permissionandroid:name="android.permission.VIBRATE"/>
  8. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  9. <activityandroid:name=".NotificationMainActivity"
  10. android:label="@string/app_name">
  11. <intent-filter>
  12. <actionandroid:name="android.intent.action.MAIN"/>
  13. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  14. </intent-filter>
  15. </activity>
  16. <activityandroid:name=".ToNotificationActivity"></activity>
  17. </application>
  18. </manifest>

更多相关文章

  1. 【支付宝】Android(安卓)支付宝支付 沙箱测试(交易状态错误ALI31
  2. Android中sqlite之从网上获取数据更新UI
  3. android ImageButton示例
  4. Android(安卓)自定义搜索框(带搜索图标、清除图标、语音图标)
  5. Android中的Activity生命周期学习
  6. Android之TelephonyManager类的方法详解
  7. Android(安卓)应用程序窗体显示状态操作(requestWindowFeature()
  8. Android高级UI_Path小试牛刀
  9. Android(安卓)8.0 SystemUI下拉状态栏快捷开关

随机推荐

  1. matplotlib绘图的核心原理讲解
  2. matplotlib绘图技巧详解(一)
  3. 异步函数中的异常处理及测试方法 [每日前
  4. Python+Kepler.gl轻松制作酷炫路径动画
  5. Python笔下那些神奇的树
  6. 一文读懂 Kubernetes APIServer 原理
  7. 利用geopandas与PostGIS进行交互
  8. Python中最好用的6个地图可视化库
  9. Matplotlib如何绘制多个子图
  10. TypeScript 3.3发布!看看增加了什么新功能