android开发之notification通知完全解析

android开发之notification通知完全解析

本文主要介绍的是notification通知的使用,通过阅读此文,你可以了解,在android开发中,notification通知各种使用方法。本文的notification主要是针对android4.4以下的版本。

现在,我们来看一下,如何实现一个notification。估计大家现在最常用的做法是下面这种:

Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.app_name),System.currentTimeMillis());Intent notificationIntent = new Intent(this, MyService.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, (this, getText(R.string.hello_world),getText(R.string.hello_world), pendingIntent);startForeground(1, notification);

这是在service里面启动一个notification,但是大家一定记住这种方法已经过时了,也就是说Google官方已经不提倡使用这种方法来启动一个notification了。官方推荐使用V4包下NotificationCompat.Builder,这样,我们便可以设置各种属性。

好了,现在我们具体来看一下,Notification的具体实现。

1.创建一个简单的notification。

Required 必须要包含的内容A Notification object must contain the following:一个notification必须包含下面的属性:A small icon, set by setSmallIcon()一个small icon,用setSmallIcon()来设置,对应于上图中的2号区域A title, set by setContentTitle()一个title,用setContentTitle()来设置,对应于上图中的1号区域Detail text, set by setContentText()详细文本,用setContentText()来设置,对应于上图中的3号区域

这三个是必须设置的,至于其他的扩展则是需求而定。代码如下:

private NotificationManager manager;NotificationCompat.Builder notifyBuilder;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);/*实例化NotificationManager以获取系统服务*/manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);}/** * 显示最简单的通知,以下method中的三个set方法是必须设置的 * * @param view */public void simNotification(View view) {Toast.makeText(this, "hha", Toast.LENGTH_LONG).show();notifyBuilder = new NotificationCompat.Builder(this)/*设置small icon*/.setSmallIcon(R.drawable.ic_launcher)/*设置title*/.setContentTitle("通知")/*设置详细文本*/.setContentText("Hello world");manager.notify(100, notifyBuilder.build());}

代码里的注释已经很详细了,simNotification绑定到了一个Button。这样,我们已经实现了最简单的notification。

2.创建一个点击跳转到其它Activity的Notification。无法通过左右滑动删除

/** * 点击跳转到指定Activity * * @param view */public void largePicture(View view) {/*实例化NotificationManager以获取系统服务*/manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//点击的意图ACTION是跳转到IntentIntent resultIntent = new Intent(this, MainActivity.class);resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);notifyBuilder = new NotificationCompat.Builder(this)/*设置large icon*/.setLargeIcon(bitmap)/*设置small icon*/.setSmallIcon(R.drawable.ic_launcher)/*设置title*/.setContentTitle("通知")/*设置详细文本*/.setContentText("Hello world")/*设置发出通知的时间为发出通知时的系统时间*/.setWhen(System.currentTimeMillis())/*设置发出通知时在status bar进行提醒*/.setTicker("来自问月的祝福")/*setOngoing(boolean)设为true,notification将无法通过左右滑动的方式清除* 可用于添加常驻通知,必须调用cancle方法来清除*/.setOngoing(true)/*设置点击后通知消失*/.setAutoCancel(true)/*设置通知数量的显示类似于QQ那种,用于同志的合并*/.setNumber(2)/*点击跳转到MainActivity*/.setContentIntent(pendingIntent);manager.notify(121, notifyBuilder.build());}

代码里的注释都很详细,加上前面的介绍相信大家都能理解了。

3.创建一个显示bitmap的notification,类似于屏幕截图的显示效果

/** * 类似于系统截图的效果 * * @param view */public void comNotification(View view) {/*实例化NotificationManager以获取系统服务*/manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);/*获取bitmap*/Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);notifyBuilder = new NotificationCompat.Builder(this)/*设置small icon*/.setSmallIcon(R.drawable.ic_launcher)/*设置title*/.setContentTitle("通知")/*设置详细文本*/.setContentText("Hello world").setWhen(System.currentTimeMillis()).setOngoing(true).setNumber(2);NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();bigPictureStyle.bigPicture(bitmap);notifyBuilder.setStyle(bigPictureStyle);manager.notify(121, notifyBuilder.build());}

这里唯一需要注意的是加载图片最好不要在UI线程进行(这里只是为了演示)。

4.创建一个类似于日历事件的notification,并与Service进行交互。

如果心胸不似海,又怎能有海一样的事业。

android开发之notification通知完全解析

相关文章:

你感兴趣的文章:

标签云: