work hard work smart

专注于Java后端开发。 不断总结,举一反三。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Android Notication的使用

Posted on 2013-03-17 21:10  work hard work smart  阅读(932)  评论(0编辑  收藏  举报

一、Android Notication的使用

	private void sendNotification() {
		// TODO Auto-generated method stub
		NotificationManager manager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification();
		notification.icon = R.drawable.ic_launcher;
		notification.tickerText = "I am in the state bar!";
		notification.audioStreamType = android.media.AudioManager.ADJUST_LOWER;
		
		Intent intent = new Intent(this, secondActivity.class );
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 
				0, intent, PendingIntent.FLAG_ONE_SHOT);
		
		notification.setLatestEventInfo(this, "Content Title","Content", pendingIntent);
		manager.notify(1,notification);
				
	}

代码解释:

1:获取NotificationManager:

NotificationManager m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);

2:定义一个Notification:

  Notification  m_Notification=new Notification();

3:设置Notification的各种属性:

//设置通知图标

m_Notification.icon=R.drawable.icon;               

  //当我们点击通知时显示的内容

m_Notification.tickerText="Button1 通知内容.....";                                

//通知时发出默认声音

m_Notification.defaults=Notification.DEFAULT_SOUND;

//设置通知显示参数

Intent   m_Intent=new Intent(NotificationDemo.this,DesActivity.class);      

PendingIntent m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0);

m_Notification.setLatestEventInfo(NotificationDemo.this, "Button1", "Button1通知",m_PendingIntent );

//开始执行通知

m_NotificationManager.notify(0,m_Notification);

4:既然增加能同样能删除。

  m_NotificationManager.cancel(0);   

  这个0是一个ID号,和notify第一个参数0一样。

 

二、Android Notification自定义布局实现

http://blog.csdn.net/nature_day/article/details/8659714