返回顶部

收藏

Android下Notification(通知栏)的使用

更多

Android下Notification(通知栏)的使用,像手机QQ一样实现通知栏的图标显示。

[Java]代码

public class NotificationActivity extends Activity {
 //声明通知(消息)管理器 
 NotificationManager m_NotificationManager;
 Intent  m_Intent;
 PendingIntent m_PendingIntent;
 //声明Notification对象
 Notification  m_Notification;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.notification);
  //初始化NotificationManager对象 
  m_NotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 }

 public void click(View view){
  switch (view.getId()) {
  case R.id.btn_notification_start:
   showNotification();
   break;
  case R.id.btn_notification_end:
   cancelNotification();
   break;
  default:
   break;
  }
 }
 /** 设置 */
 private void showNotification(){ 
  //点击通知时转移内容 
  //Intent intent = new Intent(this, this.getClass());
  //intent.addCategory(WINDOW_SERVICE);
  //主要是设置点击通知时显示内容的类 
  m_PendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, getIntent(), 0); //如果转移内容则用m_Intent();
  //构造Notification对象 
  m_Notification = new Notification(); 
  //设置通知在状态栏显示的图标 
  m_Notification.icon = R.drawable.icon;
  //当我们点击通知时显示的内容 
  m_Notification.tickerText = "开始登陆系统..........."; 
  //通知时发出默认的声音 
  m_Notification.defaults = Notification.DEFAULT_SOUND; 
  //设置通知显示的参数 
  m_Notification.setLatestEventInfo(NotificationActivity.this, "MyIMSYS", "登陆通知", m_PendingIntent); 
  //可以理解为执行这个通知 
  m_NotificationManager.notify(0, m_Notification); 
 }
 /** 取消 */
 private void cancelNotification(){
  m_NotificationManager.cancelAll();
 }

标签:Android,Notification

收藏

0人收藏

支持

0

反对

0

发表评论