Android中通知栏Notification详解以及自定义Notification

手机通知栏中的显示效果


一 直接使用Notification

其相关属性:

audioStreamType 当声音响起时,所用的音频流的类型 
contentIntent 当通知条目被点击,就执行这个被设置的Intent. 
contentView 当通知被显示在状态条上的时候,同时这个被设置的视图被显示. 
defaults 指定哪个值要被设置成默认的. 
deleteIntent 当用户点击"Clear All Notifications"按钮区删除所有的通知的时候,这个被设置的Intent被执行. 
icon 状态条所用的图片. 
iconLevel 假如状态条的图片有几个级别,就设置这里. 
ledARGB LED灯的颜色. 
ledOffMS LED关闭时的闪光时间(以毫秒计算) 
ledOnMS LED开始时的闪光时间(以毫秒计算) 
number 这个通知代表事件的号码 
sound 通知的声音 
tickerText 通知被显示在状态条时,所显示的信息 
vibrate 振动模式. 
when 通知的时间戳. 

1.默认声音

   notification.defaults |= Notification.DEFAULT_SOUND; 
   如果要使用自定义声音,那么就要用到sound了。如下: 
   soundnotification.sound=Uri.parse("file:///sdcard/notification/apple.mp3"); 
   需要注意一点,如果default、sound同时出现,那么sound无效,会使用默认铃声。 
2.震动

  如果是使用默认的振动方式,那么同样也是使用default。 
  notification.defaults|=Notification.DEFAULT_VIBRATE; 
  自己定义振动形式,这边需要用到Long型数组
  long[]vibrate={0,100,200,300}; 
  notification.vibrate=vibrate; 
  这边的Long型数组中,第一个参数是开始振动前等待的时间,第二个参数是第一次振动的时间,第三个参数是第二     次振动的时间,以此类推,随便定义多长的数组。但是采用这种方法,没有办法做到重复振动。 
   同样,如果default、vibrate同时出现时,会采用默认形式。

【说明】:加入手机震动,一定要在manifest.xml中加入权限:
<uses-permission android:name="android.permission.VIBRATE" />
3.闪光

  使用默认的灯光
   notification.defaults|=Notification.DEFAULT_LIGHTS; 
   自定义闪光
   notification.ledARGB=0xff00ff00; 
   notification.ledOnMS=300; 
   notification.ledOffMS=1000; 
   notification.flags|=Notification.FLAG_SHOW_LIGHTS; 
   其中ledARGB表示灯光颜色、ledOnMS亮持续时间、ledOffMS暗的时间。 
   【说明】:这边的颜色跟设备有关,不是所有的颜色都可以,要看具体设备。
    DEFAULT_ALL    使用所有默认值,比如声音,震动,闪屏等等
4.NotificationManager常用方法介绍:
public void cancelAll() 移除所有通知(只是针对当前Context下的Notification)
public  void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)
public  void notify(String tag ,int id, Notification notification) 将通知加入状态栏,标签为tag,标记为id
public  void notify(int id, Notification notification) 将通知加入状态栏,标记为id

5.系统默认样式通知代码

protected void setNotification() {
		// TODO Auto-generated method stub
		id++;
		Notification notification=new Notification();
		notification.icon=R.drawable.a;//通知图标
		notification.tickerText="来了一条消息";//状态栏显示的通知文本提示
		notification.when=System.currentTimeMillis();//通知产生的时间,会在通知信息里显示
		notification.flags=Notification.FLAG_AUTO_CANCEL;//点击后消失
	    notification.flags |= Notification.FLAG_SHOW_LIGHTS; //LED灯闪烁
        notification.defaults |= Notification.DEFAULT_LIGHTS; 
        notification.defaults |= Notification.DEFAULT_SOUND; //发出提示音
        notification.defaults |= Notification.DEFAULT_VIBRATE;

		Intent intent=new Intent(this,ShowMainActivity.class);
		PendingIntent pendingIntent=PendingIntent.getActivity(this,
				0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		notification.setLatestEventInfo(this, "contentTitle", "contentText", pendingIntent);
		NotificationManager manager=(NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
		manager.notify(id, notification);
	}

1>在设置图标时,ic_launcher.png(随意自己的图标,只是名字ic_launcher.png)手机顶部状态栏提示的图标和通知栏的图标显示的就是这个ic_launcher.png,图标是一致的,大部分应用也是一致处理的;倘若手机顶部状态栏提示图标和通知栏的图标不想一样,那么如上代码notification.icon=R.drawable.a;这里是状态栏的提示图标,通知栏中显示的是系统默认的ic_launcher.png,想换什么图标都行,名字不能改,同时drawable下的都要更换,小米手机上是这样,但是在酷派上提示图标和通知栏图标显示的都是notification.icon=R.drawable.ic_launcher;不知道为什么;

2>manager.notify(id, notification);若id设置为个常量,那么通知栏只显示一条通知,若要每条都显示,定义static int id=0;进行id++;最后赋id到此方法中,如上代码;

3>PendingIntent是表示一个在将来某个待定的时刻发生,而Intent是立刻发生;

    PendingIntent支出三种特定的意图:启动Activity,启动Service,发生广播,对应方法如下:

PendingIntent pendingIntent=PendingIntent.getActivity(context, requestCode, intent, flags);

PendingIntent pendingIntent=PendingIntent.getService(context, requestCode, intent, flags);

PendingIntent pendingIntent=PendingIntent.getService(context, requestCode, intent, flags);

二.使用RemoteViews自定义样式通知

//自定义通知
	protected void setSelfNotification() {
		// TODO Auto-generated method stub
		sid++;
		Notification notification=new Notification();
		notification.icon=R.drawable.b;//通知图标
		notification.tickerText="来了一条消息";//状态栏显示的通知文本提示
		notification.when=System.currentTimeMillis();//通知产生的时间,会在通知信息里显示
		notification.flags=Notification.FLAG_AUTO_CANCEL;//点击后消失
		notification.flags |= Notification.FLAG_SHOW_LIGHTS; //LED灯闪烁
		notification.defaults |= Notification.DEFAULT_LIGHTS; 
		notification.defaults |= Notification.DEFAULT_SOUND; //发出提示音
		notification.defaults |= Notification.DEFAULT_VIBRATE;

		Intent intent=new Intent(this,ShowMainActivity.class);
		PendingIntent pendingIntent=PendingIntent.getActivity(this,
				0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		
		RemoteViews remoteViews=new RemoteViews(getPackageName(), R.layout.remote_view);
		remoteViews.setTextViewText(R.id.tv_title, "火影");
		remoteViews.setTextViewText(R.id.tv_content, "鸣人大战佩恩");
		remoteViews.setImageViewResource(R.id.icon, R.drawable.b);
		Intent intent2=new Intent(this,ShowTwoMainActivity.class);
		//remoteViews的意图
		PendingIntent pendingIntent2=PendingIntent.getActivity(
				this, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
		//给我remoteViews上的控件tv_content添加监听事件
		remoteViews.setOnClickPendingIntent(R.id.tv_content, pendingIntent2);
		
		notification.contentView=remoteViews;
		notification.contentIntent=pendingIntent;
		NotificationManager manager=(NotificationManager)
				getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
		manager.notify(sid, notification);
		
	}

点击通知栏会调转到helloworld界面,点击通知内容会跳转另一个showtwo界面,意思就是这里可以设置自定义布局中控件的监听事件,通过remoteViews.setOnClickPendingIntent(R.id.tv_content, pendingIntent2);

三 使用Notification.Builder

     这是3.0中引入的,简化了配置Notification的标志,选项,内容,布局过程

protected void setNotificationBuilder() {
		// TODO Auto-generated method stub
		id++;
		Notification.Builder builder=new Notification.Builder(this);
		builder.setSmallIcon(R.drawable.ic_launcher)
		.setTicker("大蛇丸")
		.setWhen(System.currentTimeMillis())
		.setContentTitle("鸣人")
		.setContentText("分身术")
		.setContentInfo("火影")
		.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE)
		.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
		.setVibrate(new long[]{1000,1000,1000,1000})
		.setLights(Color.RED, 0, 1);
		Notification notification=builder.build();
		NotificationManager manager=(NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
		manager.notify(id, notification);
	}

四 带有进度条的通知

	protected void setNotificationProgrss() {
		// TODO Auto-generated method stub
		id++;
		Intent intent=new Intent(this,ShowMainActivity.class);
		PendingIntent pendingIntent=PendingIntent.getActivity(this,
				0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		Notification.Builder builder=new Notification.Builder(this);
		builder.setSmallIcon(R.drawable.ic_launcher)
		.setTicker("大蛇丸")
		.setWhen(System.currentTimeMillis())
		.setContentTitle("卡卡西")
		.setContentText("卡卡西和带土")
		.setContentInfo("火影")
		.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE)
		.setProgress(100, 50, false)
		.setContentIntent(pendingIntent);
		Notification notification=builder.build();
		NotificationManager manager=(NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
		manager.notify(id, notification);
	}


代码下载


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章