首页 > 其他 > 详细

状态栏通知

时间:2014-03-14 18:44:20      阅读:468      评论:0      收藏:0      [点我收藏+]

 

 

   1:  package com.example.notification;
   2:   
   3:  import android.os.Bundle;
   4:  import android.app.Activity;
   5:  import android.app.Notification;
   6:  import android.app.NotificationManager;
   7:  import android.app.PendingIntent;
   8:  import android.content.Context;
   9:  import android.view.Menu;
  10:  import android.view.View;
  11:  import android.widget.Button;
  12:   
  13:  public class MainActivity extends Activity {
  14:   
  15:      @Override
  16:      protected void onCreate(Bundle savedInstanceState) {
  17:          super.onCreate(savedInstanceState);
  18:          setContentView(R.layout.activity_main);
  19:          Button button = (Button)findViewById(R.id.button);
  20:          
  21:          button.setOnClickListener(new View.OnClickListener() {
  22:              
  23:              @Override
  24:              public void onClick(View v) {
  25:                  // TODO Auto-generated method stub
  26:                  //获取通知管理器
  27:                  NotificationManager     mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  28:                  
  29:                 // 获取一个图片的索引。 该图片是系统自带的。 当然也可以自己在 res 中添加图片, 然后
  30:                  //再进行索引。Android 系统中带有很多的图片,可以用上述方式进行引用。 
  31:                  int icon = android.R.drawable.sym_action_email;
  32:                  
  33:                  long when = System.currentTimeMillis();
  34:                  
  35:                  //新建一个通知,指定图标和标题
  36:                 // Notification notification = new Notification(int icon, CharSequence tickerText, long when);
  37:                  Notification notification = new Notification(icon,null,when);
  38:                  //设置通知的声音。但在 AVD 上没有声音,因此,无法听到。
  39:                  notification.defaults = Notification.DEFAULT_SOUND;//发出系统默认声音
  40:                  //当点击消息时,会向系统发送openintent意图,即将发生的意图
  41:                  PendingIntent cIntent = PendingIntent.getActivity(MainActivity.this,0,null,0);
  42:                  //该行代码设置通知的相关信息
  43:                  notification.setLatestEventInfo(MainActivity.this, "开会通知", "今天下午四点开会,请及时参加", cIntent);
  44:                  //调用通知管理器的 notify 方法发出该通知
  45:                  mNotificationManager.notify(0, notification);   
  46:              }
  47:          });
  48:      }
  49:   
  50:      @Override
  51:      public boolean onCreateOptionsMenu(Menu menu) {
  52:          // Inflate the menu; this adds items to the action bar if it is present.
  53:          getMenuInflater().inflate(R.menu.main, menu);
  54:          return true;
  55:      }
  56:   
  57:  }

 

说明:
? NotificationManager mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
该行代码的作用是获取通知管理器。
getSystemService 方法是获取系统服务,其参数 Context.NOTIFICATION_SERVICE 是
Context 类中的静态常量。返回的类型是 Object,此行代码返回的是
NotificationManager 类型,加以强转就得到一个通知管理器。
在 Eclipse 中,按住“Ctrl”键,同时用鼠标点击代码 NOTIFICATION_SERVICE,就可
以查看它在 Context 类中的定义如下:
public static final String NOTIFICATION_SERVICE = "notification";。
? int icon = android.R.drawable.sym_action_email;
获取一个图片的索引。 该图片是系统自带的。 当然也可以自己在 res 中添加图片, 然后
再进行索引。Android 系统中带有很多的图片,可以用上述方式进行引用。
? long when = System.currentTimeMillis();
获取系统时间
? Notification notification = new Notification(int icon, CharSequence tickerText,
long when);
新建一个通知。参数介绍如下:
icon : 该通知将采用的图片
tickerText : 通知标题,该通知的标题将在后面设置,因此此处置空
when : 通知的时间
? notification.defaults = Notification.DEFAULT_SOUND;
设置通知的声音。但在 AVD 上没有声音,因此,无法听到。
? PendingIntent contentIntent =
PendingIntent.getActivity(NotificationActivity.this, 0, null, 0);
创建一个 contentIntent。
? notification.setLatestEventInfo (Context context, CharSequence contentTitle,
CharSequence contentText, PendingIntent contentIntent)
《大话企业级 Android 开发》 本教程官方讨论群:65882321
该行代码设置通知的相关信息。
context : 应用上下文
contentTitle : 通知的标题
contentText : 通知的内容
contentIntent : 当该通知被点击时,发出的意图。
? mNotificationManager.notify(int id, Notification notification);
调用通知管理器的 notify 方法发出该通知。其中:
id : 要发送的通知的标识码
notification: 要发送的通知

 

 

bubuko.com,布布扣

 

bubuko.com,布布扣

状态栏通知,布布扣,bubuko.com

状态栏通知

原文:http://www.cnblogs.com/zhuxuekui/p/3598969.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!