您的位置:首页 > 移动开发 > Android开发

Android学习——Notification

2016-05-16 19:13 393 查看
一、Notification:(一)、简介:显示在手机状态栏的通知。Notification所代表的是一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification。Android3.0增加了Notification.Builder类,该类可以轻松地创建Notification对象。Notification,俗称通知,是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容。  【注意:】因为一些Android版本的兼容性问题,对于Notification而言,Android3.0是一个分水岭,如果不考虑向下兼容的问题,构建Notification使用Notification.Builder构建,如果考虑到向下兼容性的问题,一般推荐使用NotificationCompat.Builder构建(NotificationCompat类在v4包中:android.support.v4.app.NotificationCompat)。通知一般通过NotificationManager服务来发送一个Notification对象来完成,NotificationManager是一个重要的系统级服务,该对象位于应用程序的框架层中,应用程序可以通过它像系统发送全局的通知。这个时候需要创建一个Notification对象,用于承载通知的内容。但是一般在实际使用过程中,一般不会直接构建Notification对象,而是使用它的一个内部类NotificationCompat.Builder来实例化一个对象(Android3.0之下使用Notification.Builder),并设置通知的各种属性,最后通过NotificationCompat.Builder.build()方法得到一个Notification对象。当获得这个对象之后,可以使用NotificationManager.notify()方法发送通知。  NotificationManager类是一个通知管理器类,这个对象是由系统维护的服务,是以单例模式获得,所以一般并不直接实例化这个对象。在Activity中,可以使用Activity.getSystemService(String)方法获取NotificationManager对象,Activity.getSystemService(String)方法可以通过Android系统级服务的句柄,返回对应的对象。在这里需要返回NotificationManager,所以直接传递Context.NOTIFICATION_SERVICE即可。  虽然通知中提供了各种属性的设置,但是一个通知对象,有几个属性是必须要设置的,其他的属性均是可选的,必须设置的属性如下:小图标,使用setSamllIcon()方法设置。标题,使用setContentTitle()方法设置。文本内容,使用setContentText()方法设置。(二)、Notification.Builder类中提供的方法:builder.setAutoCancel(); 设置点击通知后,状态栏自动删除通知。builder.setSmallIcon(R.drawable.alert); 设置通知小图标builder.setLargeIcon(R.drawable.alert2); 设置通知大图标builder.setContentTitle("标题"); 设置通知标题builder.setContentText("文本"); 设置通知内容builder.setDefaults(Notification.DEFAULT_SOUND| Notification.DEFAULT_VIBRATE); 设置通知的音乐、振动、LED等。builder.setSound(); 设置通知的音乐builder.setTicker(); 设置通知在状态栏的提示文本。第一次提示消息的时候显示在通知栏上。builder.setContentIntent(); 设置点击通知后将要启动的程序组件对应的PendingIntent。(三)、发送Notification的步骤:(四部曲)1、调用getSystemService(NOTIFICATION_SERVICE)方法获取系统的NotificationManager服务,它是一个重要的系统服务。应用程序可以通过NotificationManager 向系统发送全局通知;2、构造Notification.Builder对象;3、设置Notification.Builder对象的各种属性;4、通过NotificationManager 的notify()方法发送Notification。(四)、示例代码:核心代码如下:
public void clickButton(View view) {
switch (view.getId()) {
case R.id.button_main_common:
Notification.Builder builder1 = new Notification.Builder(this);    //t推荐使用Notificationcompat.Builder
builder1.setSmallIcon(R.drawable.ic_launcher);
builder1.setContentTitle("提示:");
builder1.setContentText("请注意休息,时间到了!");
builder1.setAutoCancel(true);

Intent intent = new Intent(this, NextActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent,
PendingIntent.FLAG_ONE_SHOT);
builder1.setContentIntent(pIntent);

nManager.notify(R.id.button_main_common, builder1.build());
break;case R.id.button_main_bigpicture:Notification.Builder builder2 = new Notification.Builder(this);builder2.setSmallIcon(R.drawable.alert);builder2.setContentTitle("提示:");builder2.setContentText("以下是展示的大图片。。。");Notification.BigPictureStyle bigStyle = new BigPictureStyle(builder2);Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.big);bigStyle.bigPicture(bitmap);nManager.notify(R.id.button_main_bigpicture, bigStyle.build());break;
//进度条通知case R.id.button_main_progress:final Notification.Builder builder3 = new Notification.Builder(this);builder3.setSmallIcon(R.drawable.alert);builder3.setContentTitle("提示:");builder3.setContentText("数据下载中。。。");new Thread(new Runnable() {@Overridepublic void run() {for (int i = 0; i <= 100; i += 5) {builder3.setProgress(100, i, false);nManager.notify(R.id.button_main_progress,builder3.build());try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}}builder3.setContentText("下载完毕!");nManager.notify(R.id.button_main_progress, builder3.build());nManager.cancel(R.id.button_main_progress);}}).start();break;case R.id.button_main_clear:nManager.cancelAll();break;}}
5、自定义notification效果图:并对中间的播放按钮做了一个简单的点击处理事件(点击播放后,请关闭幕帘否则可能会看不到toast提示)
 private void showCustomView() {RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.custom_notification);Intent intent = new Intent(this, TestMusicControl.class);PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,intent, 0);remoteViews.setOnClickPendingIntent(R.id.paly_pause_music,pendingIntent);NotificationCompat.Builder builder = new Builder(context);builder.setContent(remoteViews).setSmallIcon(R.drawable.music_icon).setLargeIcon(icon).setOngoing(true).setTicker("music is playing");manager.notify(NOTIFICATION_ID_8, builder.build());}
布局文件:
 <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_vertical"android:orientation="horizontal" ><ImageViewandroid:id="@+id/songer_pic"android:layout_width="64dp"android:layout_height="64dp"android:src="@drawable/yan" /><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center_vertical"android:orientation="horizontal" ><ImageViewandroid:id="@+id/last_music"android:layout_width="0dp"android:layout_height="48dp"android:layout_weight="1"android:src="@drawable/music_previous" /><ImageViewandroid:id="@+id/paly_pause_music"android:layout_width="0dp"android:layout_height="48dp"android:layout_weight="1"android:src="@drawable/music_play" /><ImageViewandroid:id="@+id/next_music"android:layout_width="0dp"android:layout_height="48dp"android:layout_weight="1"android:src="@drawable/music_next" /></LinearLayout></LinearLayout>
(五)、PendingIntent:1、PendingIntent字面意义:等待的,悬而未决的Intent;Intent一般是用作Activity、Sercvice、BroadcastReceiver之间传递数据,而Pendingintent,一般用在Notification上,可以理解为延迟执行的intent,PendingIntent是对Intent一个包装;2、得到一个 PendingIntent 对象,使用方法类的静态方法 getActivity(Context, int, Intent, int);3、PendingIntent是一种特殊的Intent。主要的区别在于Intent是立刻执行,而 PendingIntent 的执行不是立刻,而是当条件满足后才发送企图,而且PendingIntent 可以取消;4、PendingIntent执行的操作实质上是参数传进来的Intent的操作,使用 PendingIntent 的目的在于它所包含的Intent的操作的执行是需要满足某些条件的。5、主要的使用的地方和例子:通知Notificatio的发送,短消息SmsManager的发送和警报器AlarmManager的执行等。总而言之,PendingIntent就是一个可以在满足一定条件下执行的Intent,它相比于Intent的优势在于自己携带有Context对象,这样他就不必依赖于某个activity才可以存在。(六)、Intent和PendingIntent的区别:【掌握,以备面试之需】Intent是立即使用的,而PendingIntent可以等到事件发生后触发,PendingIntent可以cancel;Intent在程序结束后即终止,而PendingIntent在程序结束后依然有效;PendingIntent自带Context,而Intent需要在某个Context内运行;Intent在原task中运行,PendingIntent在新的task中运行。(七)、PendingIntent的几个常量:(getActivity(Context,int, Intent, int)方法中的第四个参数)FLAG_ONE_SHOT : 这个PendingIntent只能使用一次。FLAG_NO_CREATE : 如果被描述的PendingIntent不存在,那么简单地返回null,而不是创建它。FLAG_CANCEL_CURRENT : 如果被描述的PendingIntent已经存在,在即将生成一个新的PendingIntent前,当前的一个要被取消。FLAG_UPDATE_CURRENT :如果被描述的PendingIntent已经存在,那么继续保持它,但它其中的数据会因为新Intent而更新。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: