`
memewry
  • 浏览: 11621 次
  • 性别: Icon_minigender_1
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

android notification的用法

 
阅读更多

原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。否则将追究法律责任。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的使用介绍完毕,预祝大家成功。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics