关于Notification的使用和参数详解

今天在做关于Notification通知栏的开发,发现总是有这样或者那样的问题,最后下决心寻找关于Notification参数的一些设置。
Notification我们可以理解为有两种:
1、使用系统默认的样式;2、自定义;


像这种系统提供的东西大家都能理解:默认的不好看,但是简单方便;自定义的,好看,但容易出问题。但是基本上所有的项目都会使用自定义的东西,所有我们要好好的查看下我们所要设置的参数们。


1、使用系统默认的样式步骤及参数详解:


// 创建一个NotificationManager的引用,就是从系统中获取到它的实例


		NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


		// 如果你不是在acitivity中或者是service中你可以在getSystemService之前加上context对象


		// NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);


		// 初始化Notification


		Notification notification = new Notification(R.drawable.icon,
				"Hello,there!", System.currentTimeMillis());
		
		/*
		 * 定义Notification的各种属性
		 * 
		 * R.drawable.icon; //通知图标
		 * 
		 * "Hello,there!"; //状态栏显示的通知文本提示,就是在顶部显示一下的文字
		 * 
		 * System.currentTimeMillis(); //(获取系统时间)通知产生的时间,会在通知信息里显示
		 */


		// 使用提示声音进行通知用户


		notification.defaults = Notification.DEFAULT_SOUND;


		/*
		 * 也可以使用uri让用户选择铃音
		 * 
		 * notification.sound = Uri.parse("file:///sdcard/XXXX.mp3");
		 * 
		 * notification.sound =
		 * Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
		 * 
		 * 如果想要让声音持续重复直到用户对通知做出反应,则可以在notification的flags字段增加"FLAG_INSISTENT"
		 * 
		 * 如果notification的defaults字段包括了"DEFAULT_SOUND"属性,则这个属性将覆盖sound字段中定义的声音
		 */


		// 添加振动提示功能
		notification.defaults |= Notification.DEFAULT_VIBRATE;


		/*
		 * 或者可以定义自己的振动模式:
		 * 
		 * long[] vibrate = {0,100,200,300};
		 * //0毫秒后开始振动,振动100毫秒后停止,再过200毫秒后再次振动300毫秒
		 * 
		 * notification.vibrate = vibrate;
		 * 
		 * long数组可以定义成想要的任何长度
		 * 
		 * 如果notification的defaults字段包括了"DEFAULT_VIBRATE",则这个属性将覆盖vibrate字段中定义的振动
		 */


		// 添加LED灯提醒
		notification.defaults |= Notification.DEFAULT_LIGHTS;


		/*
		 * 或者可以自己的LED提醒模式:
		 * 
		 * notification.ledARGB = 0xff00ff00;
		 * 
		 * notification.ledOnMS = 300; //亮的时间
		 * 
		 * notification.ledOffMS = 1000; //灭的时间
		 * 
		 * notification.flags |= Notification.FLAG_SHOW_LIGHTS;
		 */


		关键部分/
		/*
		 * 
		 * 更多的特征属性
		 * 
		 * notification.flags |= FLAG_AUTO_CANCEL; //在通知栏上点击此通知后自动清除此通知
		 * 
		 * notification.flags |= FLAG_INSISTENT; //重复发出声音,直到用户响应此通知
		 * 
		 * notification.flags |= FLAG_ONGOING_EVENT;
		 * //将此通知放到通知栏的"Ongoing"即"正在运行"组中
		 * 
		 * notification.flags |= FLAG_NO_CLEAR; //表明在点击了通知栏中的"清除通知"后,此通知不清除,
		 * 
		 * //经常与FLAG_ONGOING_EVENT一起使用
		 * 
		 * notification.number = 1; //number字段表示此通知代表的当前事件数量,它将覆盖在状态栏图标的顶部
		 * 
		 * //如果要使用此字段,必须从1开始
		 * 
		 * notification.iconLevel = ; //
		 */
		
		// 设置通知栏下拉菜单中,点击通知后响应的事件消息
		// 这里是使用intent进行跳转,点击后进入你想要进入的页面;当然在这里你也可以做一些其他的事情;
		Intent notificationIntent = new Intent(this, MainActivity.class); // 点击该通知后要跳转的Activity


		// 初始化PendingIntent对象,用来
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);


		//这里显示的是下拉菜单后的布局
		notification.setLatestEventInfo(this, "天气预报", "晴空万里", contentIntent);


		// 把Notification传递给NotificationManager
		mNotificationManager.notify(0, notification);


当然上面的所有属性有一部分你可以选择在某些事件之后再设置,比如说:当下载完成后你要给它设置,点击后消息在通知栏中消失,这样就可以添加以下代码:
notification.flags = Notification.FLAG_AUTO_CANCEL;


当你点击了这条下拉菜单栏中的消息之后,该条消息就会消失;



2、自定义
既然是自定义,当然要你自己写布局文件了,那么怎么在notification中引入该布局文件呢?使用RemoteViews类;
要定义自己的扩展消息,首先要初始化一个RemoteViews对象,然后将它传递给Notification的contentView字段,再把PendingIntent传递给contentIntent字段。
布局文件:(content_view.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00000000"
    android:orientation="vertical" 
    android:padding="5dp">


    <ImageView 
        android:id="@+id/content_view_image"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:src="@drawable/test"
        
        />
    <TextView
        android:id="@+id/content_view_text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0%"
        android:textColor="#000000"
        android:layout_toRightOf="@id/content_view_image"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="15dp"
      />
	<ProgressBar 
	android:id="@+id/content_view_progress"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:max="100"
        android:layout_below="@id/content_view_image"
        android:layout_marginTop="4dp"
	    />
	
</RelativeLayout>


接下来的步骤就是:1、初始化notification;2、给notification添加视图;3、给视图中的控件赋值;4、点击消息所触发的事件;5、显示该条通知消息;
步骤:1和2:
初始化和上面一样,这里关键就说下添加视图吧!
updateNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
		notification = new Notification(R.drawable.test, "紫阳天气开始下载",System.currentTimeMillis());
		notification.contentView = new RemoteViews(mContext.getPackageName(), R.layout.content_view);  
		updatePendingIntent = PendingIntent.getActivity(mContext,0, new Intent(mContext, MainActivity.class), 0);
		notification.contentIntent=updatePendingIntent;

这句:
notification.contentView = new RemoteViews(mContext.getPackageName(), R.layout.content_view); 

就相当于activity中的setContentView()方法吧!这里一定要注意一点:当你使用了
notification.contentView
就一定要使用:
notification.contentIntent

否则会报:android.app.RemoteServiceException: Bad notification posted from package
步骤:3

notification.contentView.setTextViewText(R.id.content_view_text1, "进度为:"+length+"%");
		notification.contentView.setProgressBar(R.id.content_view_progress, 100, length, false); 

步骤4:
updatePendingIntent
就是你点击事件所触发的事件;这个和上面使用系统默认的一样,不做详细介绍了!

步骤5:
最后:
// 把Notification传递给

NotificationManagermNotificationManager.notify(0, notification);

完成显示;
到这里就说完了,写的不好,大家不要喷啊!







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值