原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。否则将追究法律责任。http://jackxlee.blog.51cto.com/2493058/682435


Notification和NotificationManager的操作相对比较简单,一般用来获取系统级的服务NotificationManager,然后实例化Notification的对象,设置它的一系列属性(比如说图标、时间、标题、内容等),最后通过NotificationManager发出通知即可。

The description from SDK about Notification:

        
  1. AclassthatrepresentshowapersistentnotificationistobepresentedtotheuserusingtheNotificationManager.
  2. TheNotification.BuilderhasbeenaddedtomakeiteasiertoconstructNotifications.
  3. Foraguidetocreatingnotifications,seetheCreatingStatusBarNotificationsdocumentintheDevGuide.

接下来使用一个简单的案例来演示一下Notification的使用:

首先MainActivity的代码如下:

        
  1. publicclassMainActivityextendsActivity{
  2. privateButtonbtnSend;
  3. //定义BroadcastReceiver的action
  4. privatestaticfinalStringNotificationDemo_Action="com.ceo.notification.activity.NotificationDemo_Action";
  5. /**Calledwhentheactivityisfirstcreated.*/
  6. @Override
  7. publicvoidonCreate(BundlesavedInstanceState){
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10. //getthewidgets'instance
  11. getInstance();
  12. btnSend.setOnClickListener(newView.OnClickListener(){
  13. @Override
  14. publicvoidonClick(Viewv){
  15. Intentintent=newIntent();
  16. intent.setAction(NotificationDemo_Action);
  17. sendBroadcast(intent);
  18. }
  19. });
  20. }
  21. publicvoidgetInstance(){
  22. btnSend=(Button)findViewById(R.id.btnSend);
  23. }
  24. }

相对应的main.xml布局:

        
  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:textColor="#EEE"
  12. android:textStyle="bold"
  13. android:textSize="25sp"
  14. android:text="Notification应用的小案例"
  15. />
  16. <Button
  17. android:id="@+id/btnSend"
  18. android:text="sendnotification"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. />
  22. </LinearLayout>

SecondActivity的代码如下:

        
  1. publicclassSecondActivityextendsActivity{
  2. privateButtonbtnCancel;
  3. //声明Notification
  4. privateNotificationnotification;
  5. //声明NotificationManager
  6. privateNotificationManagermNotification;
  7. //标识Notification的ID
  8. privatestaticfinalintID=1;
  9. @Override
  10. protectedvoidonCreate(BundlesavedInstanceState){
  11. //TODOAuto-generatedmethodstub
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.second);
  14. getInstance();
  15. //怎样获得NotificationManager的实例?
  16. Stringservice=NOTIFICATION_SERVICE;
  17. mNotification=(NotificationManager)getSystemService(service);
  18. //获得Notification的实例
  19. notification=newNotification();
  20. //设置该图标会在状态栏显示
  21. inticon=notification.icon=android.R.drawable.stat_notify_chat;
  22. //设置提示信息
  23. StringtickerText="TestNotification";
  24. //设置显示时间
  25. longwhen=System.currentTimeMillis();
  26. notification.icon=icon;
  27. notification.tickerText=tickerText;
  28. notification.when=when;
  29. Intentintent=newIntent(this,MainActivity.class);
  30. PendingIntentpi=PendingIntent.getActivity(this,0,intent,0);
  31. notification.setLatestEventInfo(this,"消息","HelloAndroid",pi);
  32. mNotification.notify(ID,notification);
  33. btnCancel.setOnClickListener(newView.OnClickListener(){
  34. @Override
  35. publicvoidonClick(Viewv){
  36. mNotification.cancel(ID);//--->取消通知
  37. }
  38. });
  39. }
  40. publicvoidgetInstance(){
  41. btnCancel=(Button)findViewById(R.id.btnCancel);
  42. }
  43. }

相对应的second.xml布局:

        
  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:textColor="#EEE"
  12. android:textStyle="bold"
  13. android:textSize="25sp"
  14. android:text="显示通知界面"
  15. />
  16. <Button
  17. android:id="@+id/btnCancel"
  18. android:text="cancelnotification"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. />
  22. </LinearLayout>

MyReceiver的代码如下:

        
  1. publicclassMyReceiverextendsBroadcastReceiver{
  2. @Override
  3. publicvoidonReceive(Contextcontext,Intentintent){
  4. //实例化Intent
  5. Intenti=newIntent();
  6. //在新任务中启动Activity
  7. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  8. //设置Intent启动的组件名称
  9. i.setClass(context,SecondActivity.class);
  10. //启动Activity,显示通知
  11. context.startActivity(i);
  12. }
  13. }

当然不要忘了在AndroidManifest文件中注册广播和Activity等:

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.ceo.notification.activity"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  7. <activityandroid:name=".MainActivity"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <actionandroid:name="android.intent.action.MAIN"/>
  11. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  12. </intent-filter>
  13. </activity>
  14. <receiverandroid:name="MyReceiver">
  15. <intent-filter>
  16. <actionandroid:name="com.ceo.notification.activity.NotificationDemo_Action"/>
  17. </intent-filter>
  18. </receiver>
  19. <activityandroid:name=".SecondActivity"></activity>
  20. </application>
  21. <uses-sdkandroid:minSdkVersion="8"/>
  22. </manifest>

最后直接上图:

至此Android中Notification的使用介绍完毕,预祝大家成功。

更多相关文章

  1. Android(安卓)开发学习进程0.15 adb cardview framelayout 控件
  2. Android(安卓)MVP+Retrofit(封装)+RxJava实例
  3. Android(安卓)ActionBar使用
  4. Android用户界面 UI组件--TextView及其子类(五) DigitalClock,An
  5. android在程序中打开另一个程序
  6. Android(安卓)webview最简单小例子
  7. android 的webView的透明设置
  8. Android中popuwindow中使用listview

随机推荐

  1. Android(安卓)kotlin之对象和类(2)
  2. android琐碎笔记六
  3. Best Practice For Android
  4. Android易混淆缩写笔记
  5. 五、android中解析xml
  6. android 6.0及以下获取wifi mac地址
  7. android 一些网址收藏
  8. 修改android framework 添加service
  9. android中ListView介绍
  10. Android(安卓)Studio 使用小技巧和快捷键