Notification

	//初始化NotificationManager: 
	NotificationManager nm ;
	PendingIntent m_PendingIntent; 

-------------
nm =  (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 



// Pending这个词是“挂起”、“暂挂”的意思 我就理解为一个挂起 或者暂挂的 Intent 临时的留着一会用 xh
	        // E文实在是差看不懂文档
	        m_PendingIntent = PendingIntent.getActivity(MainActivity.this, 0,  
	                intent, 0);  
			Notification notification = new Notification(); 
			  // 设置通知在状态栏显示的图标
			notification.icon = R.drawable.icon;  
            // 当我们发送通知时在状态栏显示的内容 这个显示时暂时的大家试试就知道了
			notification.tickerText = "Button1通知内容.......";  
            // 通知时发出默认的声音 这个defaults是Notification的一个int型的变量
            // DEFAULT_SOUND是Notification这个类已经定义好的常量直接用就O 了
			notification.defaults = Notification.DEFAULT_ALL;//Notification.DEFAULT_ALL
            // 设置通知显示的参数 (Context context, CharSequence contentTitle,
            // CharSequence contentText, PendingIntent contentIntent)
            // 参数1上下文对象Context 参数2 类似标题Title 这里大家试试 看看那些文字显示在那个位置明白意思就OK
			notification.setLatestEventInfo(MainActivity.this, "xhButton1",  
                    "Button1通 知", m_PendingIntent);  
            // 可以理解为执行这个通知 或者说 由NotificationManager对象或者它的一个引用把
            // 通知发出去然后就会在状态栏显示了
            // 详细信息大家可以查看文档
			nm.notify(0, notification);  



=============================
Android添加顶部通知(Notification)并处于“正在进行中”(onGoing) 

/**
* 添加顶部通知
* @author liuzhao
*/
public void AddNotification(){
//添加通知到顶部任务栏
//获得NotificationManager实例
String service = NOTIFICATION_SERVICE;
nm = (NotificationManager)getSystemService(service);
//实例化Notification
n = new Notification();
//设置显示图标
int icon = R.drawable.ic_launcher_home;
//设置提示信息
String tickerText = “我的程序”;
//显示时间
long when = System.currentTimeMillis(); 
 

n.icon = icon;
n.tickerText = tickerText;
n.when = when;
//显示在“正在进行中”///关键
n.flags = Notification.FLAG_ONGOING_EVENT;


notification.defaults = Notification.DEFAULT_SOUND| Notification.DEFAULT_LIGHTS;    
		   
		//可以指定为震动//需要权限<uses-permission android:name="android.permission.VIBRATE" /> 
		//notification.defaults = Notification.DEFAULT_VIBRATE;
		
		   
		//可以指定为闪光灯    
		//notification.defaults = Notification.DEFAULT_LIGHTS;    



//实例化Intent
Intent intent = new Intent(tykmAndroid.this,tykmAndroid.class);
//获得PendingIntent
PendingIntent pi = PendingIntent.getActivity(tykmAndroid.this, 0, intent, 0);
//设置事件信息,显示在拉开的里面
n.setLatestEventInfo(tykmAndroid.this, “我的软件”, “我的软件正在运行……”, pi);
//发出通知
nm.notify(ID,n);
}

==================================================\//重写 notification 布局
PendingIntent contentIntent = PendingIntent.getActivity(
	arg0.getContext(), 
	R.string.app_name,
	i, 
	PendingIntent.FLAG_UPDATE_CURRENT);
			
RemoteViews rv = new RemoteViews(Main.this.getPackageName(), R.layout.notification_view);
rv.setImageViewResource(R.id.image, R.drawable.chat);
rv.setTextViewText(R.id.text, "Hello,there,I'm john.");
n.contentView = rv;
n.contentIntent = contentIntent;

nm.notify(R.string.app_name, n);


//
注意,如果使用了contentView,那么便不要使用Notification.setLatestEventInfo。
如果setLatestEventInfo在赋给 Notification.contentView 的代码之后,
那么contentView的效果将被覆盖,显示的便是 setLatestEventInfo 的效果;
如果 setLatestEventInfo 在 Notification.contentView 的代码之前,
那么显示的便是 Notification.contentView 的效果,
也就是说不管你想要setLatestEventInfo 或 contentView 的自定义效果,
请保证始终只有一句设置代码,因为在最后一句绑定的时候,
之前的设置contentView或setLatestEventInfo的代码都是完全没有必要的。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值