如何丰富程序Notification的使用
扫描二维码
随时随地手机看文章
public class
Notification
extends Object
implements Parcelable
java.lang.Object
↳
android.app.Notification
Class Overview
A class that represents how a persistent notification is to be presented to the user using theNotificationManager
.
The Notification.Builder
has been added to make it easier to construct Notifications.
1、创建NotificationManager对Notification进行管理:
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
2、创建Notification:
Notification mNotification = new Notification.Builder(getApplicationContext()) .setContentTitle("notification") .setContentText("content") .setSmallIcon(R.drawable.ic_launcher) .setLargeIcon(mBitmap) .setContentIntent(mPendingIntent) .setDefaults(Notification.DEFAULT_ALL) .build();
3、用NotificationManager的notify()方法将通知显示:
//void android.app.NotificationManager.notify(int id, Notification notification) //id: An identifier for this notification unique within your application. mNotificationManager.notify(2,mNotification);
此时Notification还不能响应点击。
接下来用PendingIntent实现点击:
Intent intent = new Intent(this,NotificationActivity.class); //PendingIntent android.app.PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags) PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
用Notification.Builder.setContentIntent():
Notification mNotification = new Notification.Builder(getApplicationContext()) .setContentIntent(mPendingIntent)
部分关键代码:
//getResources()在onCreate()里,否则报空指针context为空 //Resources android.content.Context.getResources() Intent intent = new Intent(this,NotificationActivity.class); //PendingIntent android.app.PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags) PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); //将qq.jpg转化为Bitmap,使用它设置大图标 Bitmap mBitmap=BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.qq); NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); Notification mNotification = new Notification.Builder(getApplicationContext()) .setContentTitle("notification") .setContentText("content") .setSmallIcon(R.drawable.ic_launcher) .setLargeIcon(mBitmap) .setContentIntent(mPendingIntent) .setDefaults(Notification.DEFAULT_ALL) .build(); /*int DEFAULT_ALL Use all default values (where applicable). int DEFAULT_LIGHTS Use the default notification lights. int DEFAULT_SOUND Use the default notification sound. int DEFAULT_VIBRATE Use the default notification vibrate. */ /*setLights(int argb, int onMs, int offMs) Set the desired color for the indicator LED on the device, as well as the blink duty cycle (specified in milliseconds). setSound(Uri sound) Set the sound to play. setVibrate(long[] pattern) Set the vibration pattern to use. long[] vibrates={0,1000,1000,1000} notification到来时震动1s停止1s再震动1s vibrates[0]:静止时长 vibrates[1]:振动时长 vibrates[2]:静止时长 .... */ //void android.app.NotificationManager.notify(int id, Notification notification) //id: An identifier for this notification unique within your application. mNotificationManager.notify(2,mNotification);
将qq.jpg转化为Bitmap
Bitmap mBitmap=BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.qq);
还可以设置Notification到来时的振动,声音,灯光效果
Notification.Builder
setLights(int argb, int onMs, int offMs)Set the desired color for the indicator LED on the device, as well as the blink duty cycle (specified in milliseconds).
Notification.Builder
setSound(Uri sound)Set the sound to play.
Notification.Builder
setVibrate(long[] pattern)Set the vibration pattern to use.
或者:
Notification.Builder
setDefaults(int defaults)Set which notification properties will be inherited from system defaults.