首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

状态栏揭示Notification

2012-08-26 
状态栏提示Notification当有未接电话或者短信时,在手机顶部状态栏就会出现一个小图标,提示用户没有处理的

状态栏提示Notification
当有未接电话或者短信时,在手机顶部状态栏就会出现一个小图标,提示用户没有处理的快讯,android提供了NotificationManager来管理状态栏信息,提供Notification处理这些快讯信息
示例
main.xml



main2.xml


Activity.java
public class Test_Notification extends Activity {      /** Called when the activity is first created. */      private Button btn1;      private Notification notification;      private NotificationManager notificationManager;      private Intent intent;      private PendingIntent pendIntent;        @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          btn1 = (Button) this.findViewById(R.id.Button01);          notificationManager = (NotificationManager) this                  .getSystemService(NOTIFICATION_SERVICE);// 获取系统服务(消息管理)          // 点击通知时转移内容          intent = new Intent(this, Activity2.class);          // 设置点击通知时显示内容的类         // 和Intent不同,构造pendIntent是Nofication.setLastestEventInfo参数需要        pendIntent = PendingIntent.getActivity(this, 0, intent, 0);          notification = new Notification();          btn1.setOnClickListener(new OnClickListener() {                @Override              public void onClick(View v) {                  // TODO Auto-generated method stub                  notification.icon = R.drawable.img1;// 设置在状态栏显示的图标                  notification.tickerText = "Button1通知内容.......";//设置在状态栏显示的内容                  notification.defaults = Notification.DEFAULT_SOUND;// 默认的声音                  // 设置通知显示的参数                  notification.setLatestEventInfo(Test_Notification.this,                          "Button1", "Button1通知", pendIntent);                  notificationManager.notify(0, notification);// 执行通知.              }            });      }  }  

热点排行