免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1592 | 回复: 0
打印 上一主题 下一主题

Android32_Notification用法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-05 17:34 |只看该作者 |倒序浏览
Android32_Notification用法





Android系统的状态栏(Status Bar)中有一个创新UI设计,这就是可以下拉的通知提示。当系统有一些消息要通知用户时,例如,收到短信、电子邮件、有未接来电时,都会把信息作为通知(Notification)发送给用户。

实例:

Main.xml

Xml代码
  1. 1.<?xml version="1.0" encoding="utf-8"?>  
  2. 2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. 3.    android:orientation="horizontal"  
  4. 4.    android:layout_width="fill_parent"  
  5. 5.    android:layout_height="fill_parent"  
  6. 6.    >  
  7. 7.<Button android:id="@+id/shownotification"  
  8. 8.    android:layout_height="wrap_content"  
  9. 9.    android:layout_width="wrap_content"  
  10. 10.    android:text="显示通知"  
  11. 11./>  
  12. 12.<Button android:id="@+id/deletenotification"  
  13. 13.    android:layout_height="wrap_content"  
  14. 14.    android:layout_width="wrap_content"  
  15. 15.    android:text="清除通知"  
  16. 16./>  
  17. 17.</LinearLayout>  
  18. <?xml version="1.0" encoding="utf-8"?>
  19. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  20.     android:orientation="horizontal"
  21.     android:layout_width="fill_parent"
  22.     android:layout_height="fill_parent"
  23.     >
  24. <Button android:id="@+id/shownotification"
  25.         android:layout_height="wrap_content"
  26.         android:layout_width="wrap_content"
  27.         android:text="显示通知"
  28. />
  29. <Button android:id="@+id/deletenotification"
  30.         android:layout_height="wrap_content"
  31.         android:layout_width="wrap_content"
  32.         android:text="清除通知"
  33. />
  34. </LinearLayout>
  35. NotificationActivity.java
复制代码
Java代码
  1. 1.package com.android.activity;   
  2. 2.import android.app.Activity;   
  3. 3.import android.app.Notification;   
  4. 4.import android.app.NotificationManager;   
  5. 5.import android.app.PendingIntent;   
  6. 6.import android.content.Intent;   
  7. 7.import android.os.Bundle;   
  8. 8.import android.view.View;   
  9. 9.import android.view.View.OnClickListener;   
  10. 10.import android.widget.Button;   
  11. 11.public class NotificationActivity extends Activity {   
  12. 12.    private Button showNotification = null;   
  13. 13.    private Button deleteNotification = null;   
  14. 14.    //Notification管理器   
  15. 15.    private NotificationManager nm = null;   
  16. 16.    private static final int NOTIFICATION_ID = 123456;   
  17. 17.    @Override  
  18. 18.    public void onCreate(Bundle savedInstanceState) {   
  19. 19.        super.onCreate(savedInstanceState);   
  20. 20.        setContentView(R.layout.main);   
  21. 21.     //创建 NotificationManager,其中创建的nm 对象负责发出与取消 Notification   
  22. 22.        nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);   
  23. 23.        showNotification = (Button)findViewById(R.id.shownotification);   
  24. 24.        deleteNotification = (Button)findViewById(R.id.deletenotification);   
  25. 25.        showNotification.setOnClickListener(new ShowNotificationListener());   
  26. 26.        deleteNotification.setOnClickListener(new DeletedNotificationListener());   
  27. 27.    }   
  28. 28.    class ShowNotificationListener implements OnClickListener{   
  29. 29.        public void onClick(View v) {   
  30. 30.         showNotification(R.drawable.image,"图标边的文字","标题","内容");   
  31. 31.        }   
  32. 32.    }   
  33. 33.    class DeletedNotificationListener implements OnClickListener{   
  34. 34.        public void onClick(View v) {   
  35. 35.            //表示当用户点击 Clear 之后,能够清除该通知。   
  36. 36.            nm.cancel(NOTIFICATION_ID);   
  37. 37.        }   
  38. 38.    }   
  39. 39.    public void showNotification(int icon,String tickertext,   
  40. 40.            String title,String content){   
  41. 41.        //创建 Notification ,参数依次为:icon的资源id,在状态栏上展示的滚动信息,时间。   
  42. 42.        Notification notification = new Notification(icon,tickertext,System.currentTimeMillis());   
  43. 43.        //这是设置通知是否同时播放声音或振动,声音为Notification.DEFAULT_SOUND   
  44. 44.        //振动为Notification.DEFAULT_VIBRATE;   
  45. 45.        //Light为Notification.DEFAULT_LIGHTS   
  46. 46.        //全部为Notification.DEFAULT_ALL   
  47. 47.        //如果是振动或者全部,必须在AndroidManifest.xml加入振动权限   
  48. 48.        notification.defaults = Notification.DEFAULT_ALL;   
  49. 49.        //创建一个Intent,该Intent使得当用户点击该通知后发出这个Intent   
  50. 50.        //请注意,如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。   
  51. 51.        //Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的所有Activity都关掉,并把此Activity置前以避免创建Activity的实例   
  52. 52.        //Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的Task,并在该新建的Task上创建Activity   
  53. 53.        Intent intent = new Intent(this,NotificationActivity.class);   
  54. 54.        //PendingIntent 为Intent的包装,这里是启动Intent的描述,PendingIntent.getActivity 返回的PendingIntent表示,此PendingIntent实例中的Intent是用于启动 Activity 的Intent。PendingIntent.getActivity的参数依次为:Context,发送者的请求码(可以填0),用于系统发送的Intent,标志位   
  55. 55.        PendingIntent pt = PendingIntent.getActivity(this, 0,intent, 0);   
  56. 56.        notification.setLatestEventInfo(this,title,content,pt);   
  57. 57.        //启动Notification,参数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不同Notifycation,如果程序中只有一个Notification那么这里随便你填什么都可以,不过类型必须要为int),要通知的Notification   
  58. 58.        nm.notify(NOTIFICATION_ID, notification);   
  59. 59.    }   
  60. 60.}  
  61. package com.android.activity;
  62. import android.app.Activity;
  63. import android.app.Notification;
  64. import android.app.NotificationManager;
  65. import android.app.PendingIntent;
  66. import android.content.Intent;
  67. import android.os.Bundle;
  68. import android.view.View;
  69. import android.view.View.OnClickListener;
  70. import android.widget.Button;
  71. public class NotificationActivity extends Activity {
  72.         private Button showNotification = null;
  73.         private Button deleteNotification = null;
  74.         //Notification管理器
  75.         private NotificationManager nm = null;
  76.         private static final int NOTIFICATION_ID = 123456;
  77.         @Override
  78.     public void onCreate(Bundle savedInstanceState) {
  79.         super.onCreate(savedInstanceState);
  80.         setContentView(R.layout.main);
  81.      //创建 NotificationManager,其中创建的nm 对象负责发出与取消 Notification
  82.         nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  83.         showNotification = (Button)findViewById(R.id.shownotification);
  84.         deleteNotification = (Button)findViewById(R.id.deletenotification);
  85.         showNotification.setOnClickListener(new ShowNotificationListener());
  86.         deleteNotification.setOnClickListener(new DeletedNotificationListener());
  87.     }
  88.         class ShowNotificationListener implements OnClickListener{
  89.                 public void onClick(View v) {
  90.                  showNotification(R.drawable.image,"图标边的文字","标题","内容");
  91.                 }
  92.         }
  93.         class DeletedNotificationListener implements OnClickListener{
  94.                 public void onClick(View v) {
  95.                         //表示当用户点击 Clear 之后,能够清除该通知。
  96.                         nm.cancel(NOTIFICATION_ID);
  97.                 }
  98.         }
  99.         public void showNotification(int icon,String tickertext,
  100.                         String title,String content){
  101.                 //创建 Notification ,参数依次为:icon的资源id,在状态栏上展示的滚动信息,时间。
  102.                 Notification notification = new Notification(icon,tickertext,System.currentTimeMillis());
  103.                 //这是设置通知是否同时播放声音或振动,声音为Notification.DEFAULT_SOUND
  104.             //振动为Notification.DEFAULT_VIBRATE;
  105.             //Light为Notification.DEFAULT_LIGHTS
  106.             //全部为Notification.DEFAULT_ALL
  107.             //如果是振动或者全部,必须在AndroidManifest.xml加入振动权限
  108.             notification.defaults = Notification.DEFAULT_ALL;
  109.             //创建一个Intent,该Intent使得当用户点击该通知后发出这个Intent
  110.             //请注意,如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。
  111.             //Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的所有Activity都关掉,并把此Activity置前以避免创建Activity的实例
  112.             //Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的Task,并在该新建的Task上创建Activity
  113.             Intent intent = new Intent(this,NotificationActivity.class);
  114.             //PendingIntent 为Intent的包装,这里是启动Intent的描述,PendingIntent.getActivity 返回的PendingIntent表示,此PendingIntent实例中的Intent是用于启动 Activity 的Intent。PendingIntent.getActivity的参数依次为:Context,发送者的请求码(可以填0),用于系统发送的Intent,标志位
  115.             PendingIntent pt = PendingIntent.getActivity(this, 0,intent, 0);
  116.             notification.setLatestEventInfo(this,title,content,pt);
  117.             //启动Notification,参数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不同Notifycation,如果程序中只有一个Notification那么这里随便你填什么都可以,不过类型必须要为int),要通知的Notification
  118.             nm.notify(NOTIFICATION_ID, notification);
  119.     }
  120. }
复制代码
运行结果:


文字消失后:


清除通知后:

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP