爱上soso 发表于 2013-8-28 16:28

《Android Dev Guide》系列教程16:用户通知(3)创建状态栏通知

>http://www.apkbus.com/data/attachment/forum/201105/06/203057ipy7auiu44i1v7py.jpg   
接下来的会显示状态栏窗口。用户可以打开这个窗口通过下滑状态栏。或者使用menu菜单键选择。   
http://www.apkbus.com/data/attachment/forum/201105/06/203059chw00dvq737zhlcv.jpg   
   
一个activity或者Service可以初始化状态栏通知,因为activity只有在活动状态下才能执行一些命令,所以你需要从一个service来建立状态通知。当用户启动了其他程序或者设备已经休眠时,通过这种方式,通知就可以在后台被创建。你要用到这两个类:Notification类和NotificationManager类。   
Notification类来定义状态通知的属性,比如图标,提示信息,或者提示声音。NotificationManager是一个android系统的服务,来管理和运行所有通知的,他不能被实例化,你可以用getSystemService()方法获得他的句柄。当你想通知用户时,调用notify()方法即可。   
创建一个菜单栏通知:   
1-获得MotificationManager的引用。   
   
    String ns = Context.NOTIFICATION_SERVICE;   
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);2-实例化Notification:    int icon = R.drawable.notification_icon;   
CharSequence tickerText = "Hello";   
long when = System.currentTimeMillis();   
Notification notification = new Notification(icon, tickerText, when);3-定义Notification,如显示icon、目标intent等信息   
    Context context = getApplicationContext();   
CharSequence contentTitle = "My notification";   
CharSequence contentText = "Hello World!";   
Intent notificationIntent = new Intent(this, MyClass.class);   
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);   
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);4-传递给Manager.    private static final int HELLO_ID = 1;   
mNotificationManager.notify(HELLO_ID, notification);好了,一个通知写完了。NotificationManager是一个管理所有通知的系统服务。你可以通过getSystemService()方法得到她的句柄:    String ns = Context.NOTIFICATION_SERVICE;   
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 当你想发送一个状态栏通知时,通过notify(int,Notification)方法传递Notification对象给NotificationManager,第一个参数是Notification的id,第二个参数是通知的对象。id是一个唯一的标示,当你改变通知或者进行一些的活动时必须要用到它。当用户从通知窗口选择了某个通知时,会给这个通知添加一个“FLAG_AUTO_CANCEL”标记来移除这个通知。你也可以使用cancel(int)方法,通过指定通知id来取消某个通知,或者干脆cancelAll()。   
一个通知对象定义了显示在状态栏的一些细节信息,和所有通知方式,例如声音、闪光。一个状态栏通知必须包括以下几点:@ 显示在状态栏的图标@ 一个标题(除非你自定义了提示界面)@ 一个Pending Intent,即点击后要做的操作。可选项包括以下:@ 状态栏提示文本@ 提示声音@ 震动@ 闪光初学者套件?为新的通知包含了MNotification(int,CharSequence,long)构造方法和setLatestEventInfo(Context,CharSequence,CharSequence,PendingIntent)方法。这些参数是一个通知的必要参数。下面的代码片段是一个简单的例子:    int icon = R.drawable.notification_icon;      // icon from resources   
CharSequence tickerText = "Hello";            // ticker-text   
long when = System.currentTimeMillis();         // notification time   
Context context = getApplicationContext();      // application Context   
CharSequence contentTitle = "My notification";// expanded message title   
CharSequence contentText = "Hello World!";      // expanded message text   
Intent notificationIntent = new Intent(this, MyClass.class);   
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);   
// the next two lines initialize the Notification, using the configurations above   
Notification notification = new Notification(icon, tickerText, when);   
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);你可以在事件改变时修改状态栏的通知。例如,当有一条未读短信时又来了一条短信,此时状态栏应该显示新的短信。像这种情况下,添加一条洗得通知不如更改原有通知更加合理,因为后者能避免通知的混乱。因为每个通知都有一个唯一的Id,你可以通过setLatestEventInfo()方法修改通知,然后调用notify()让其显示。你可以修改每个属性,除了context和下拉状态栏时显示的标题和文本。你可以通过setLatestEventInfo()方法给contentTitle和conTentText设置文本信息,然后调用notify()方法来更新通知。(当然了你可以创建自己的布局文件,那样的话更新显示文本就没有效果了)。你可以使用默认的声音来提醒用户有一个新通知。方法是添加一个“DEFAULT_SOUND”参数。    notification.defaults |= Notification.DEFAULT_SOUND; 如果使用非默认的声音,需要传递一个URI资源引用。例如:    notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); 下个例子中,音频文件从MediaStore类中获取:    notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");这种情况下,资源id为6的音频文件时已知的,并且已添加到content的URI中。如果你不知道exact ID,你必须在MediaStore中查询所有可用的资源。参考 Content Providers文档。如果你想让提示声音一直播放知道用户响应位置,你可以添加“FLAG_INSISTENT”参数。注意:如果包含“DEFAULT_SOUND”参数,那么默认声音会覆盖其他的声音设置。你可以使用默认的震动方式提示用户:    notification.defaults |= Notification.DEFAULT_VIBRATE; 或者使用自己的振动方式,例如:    long[] vibrate = {0,100,200,300};   
notification.vibrate = vibrate;   这个数组定义了交替的震动和关闭,一毫秒为单位。第一个值是等待多久开始震动,第二个值是第一次震动的时间,第三个是停止震动的时间,以此类推。定义多长时间都行,但是不能设置为重复。注意:如果设置了默认,则其他振动方式无效。让手机闪动LED灯,你可以实现默认的闪动色绘制。或者定义自己的闪动效果。使用默认的闪光效果,代码如下:    notification.defaults |= Notification.DEFAULT_LIGHTS;定义自己的效果,可以设置颜色、定义显示的时间,一些常用的值如下:    notification.ledARGB = 0xff00ff00;   
notification.ledOnMS = 300;   
notification.ledOffMS = 1000;   
notification.flags |= Notification.FLAG_SHOW_LIGHTS;   在这个例子里,绿灯先显示300毫秒然后关闭一秒钟,设备不是支持所有的颜色,而且不是所有的设备都支持相同颜色。所以设备会按照指定值显示最接近的颜色。绿色是常见的颜色。你可以添加几个特性到你的通知中去。下面是一些有用的设置:"FLAG_AUTO_CANCEL"标记这个标记会在用户选择查看通知窗口后自动关闭通知。"FLAG_INSISTENT" 在用户响应之前一直重复"FLAG_ONGOING_EVENT"添加这个标记把通知分在“正在运行”组中,说明程序正在运行,或者后台运行,甚至当程序不可见时,例如播放音乐或者接听电话时。"FLAG_NO_CLEAR"这个标记让你的通知不会被Clear按钮所取消。对一直进行中的通知非常有用。number field标记表示当前通知所代表的事件数。这个数字显示在icon之上。如果你打算这样使用,当通知第一次建立时必须从1开始计数。如果你改变了他的值从0或其他值开始,那么他将不会显示。iconLevel field这个值指当前icon的LevelListDrawable等级。通过改变这个值,你可以让状态栏的icon显示动画。更多详细的特性和使用方法餐卡Notification类。http://www.apkbus.com/data/attachment/forum/201105/06/203101a2iaj86iab6oqboq.jpg   
默认情况下,下拉的Notification窗口包含一个标题和文本信息。setLatestEventInfo()有两个默认的参数contentTitle和contentText 。然而你通过RemoteViews也可以定义自己下拉通知视图。上卖弄的截图就显示了一个自定义布局的通知视图,包括一个TextView和ImageView。   
定义自己的通知视图,实例化RemoteViews对象并且传递给contentView。给contentIntent字段传递PendingIntent值。创建一个自定义的通知视图最好的理解方法就是写一个例子:   
1-建立xml布局文件。例如:custom_notification_layout.xml:    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
            android:orientation="horizontal"   
            android:layout_width="fill_parent"   
            android:layout_height="fill_parent"   
            android:padding="3dp"   
            >   
    <ImageView android:id="@+id/image"   
            android:layout_width="wrap_content"   
            android:layout_height="fill_parent"   
            android:layout_marginRight="10dp"   
            />   
    <TextView android:id="@+id/text"   
            android:layout_width="wrap_content"   
            android:layout_height="fill_parent"   
            android:textColor="#000"   
            />   
</LinearLayout>这是一个自定义的扩展通知的视图,但是ImageView和TextView仍然需要程序定义。RemoteViews提供一些方便的方法来让你定义content。2-下面的代码,使用RemoveViews 的方法定义image和text。然后传递RemoteViews 对象给通知的contentView字段。如下:    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);   
contentView.setImageViewResource(R.id.image, R.drawable.notification_image);   
contentView.setTextViewText(R.id.text, "Hello, this message is in a custom expanded view");   
notification.contentView = contentView;就像显示的那样,传递程序的包名,布局的资源id给RemoteViews的构造方法。然后,定义ImageView和TextView的content,使用setImageViewResource()和setTextViewText().这种情况下,传递你要设置的view对象的引用id。最后把RemoteViews 对象传递给contentView。3-因为你不需要setLatestEventInfo()了,你必须为通知定义一个intent,例如:    Intent notificationIntent = new Intent(this, MyClass.class);   
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);   
notification.contentIntent = contentIntent;   4-通知可以这样调用:    mNotificationManager.notify(CUSTOM_VIEW_ID, notification);   RemoteViews 类也包含一些方法让你轻松地添加Chronometer或者ProgressBar在你的通知view里。注意:当建立一个自定义的通知view,你必须特别小心自定义的布局来适用不同设备的分辨率。尤其这种情况下,更要特别的小心,过于复杂的布局一定要多多测试。   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
页: [1]
查看完整版本: 《Android Dev Guide》系列教程16:用户通知(3)创建状态栏通知