Notification (通知)的 新版和旧版用法
一、先来看旧版,Api 11 之前的用法:
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.mipmap.ic_launcher, "This is bitch.", System.currentTimeMillis());
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, "This is ContentTitle","This is ContentText", pi);
manager.notify(1, notification);
manager.cancel(1); // 清除通知栏上的内容,这里的 1 是通知的Id
二、Api 11之后的用法:
NotificationManager manager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
PendingIntent contentIntent = PendingIntent.getActivities(MainActivity.this, 0,
new Intent[]{new Intent(MainActivity.this, MainActivity.class)}, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.mipmap.ic_launcher) //设置状态栏里面的图标(小图标)
// .setLargeIcon(BitmapFactory.decodeResource(resource, R.mipmap.ic_launcher)) //下拉下拉列表里面的图标(大图标)
.setTicker("This is bitch.") //设置状态栏的显示的信息
.setWhen(System.currentTimeMillis()) //设置时间发生时间
.setAutoCancel(true) //设置可以清除
.setContentTitle("This is ContentTitle") //设置下拉列表里的标题
.setContentText("This is ContentText"); //设置上下文内容
Notification notification = builder.getNotification();
manager.notify(1, notification);
Notification notification = builder.geNotification(); // 这里的方法其实已经被废弃了,它里面的源代码就是如下这样
/**
* @deprecated Use {@link #build()} instead.
*/
@Deprecated
public Notification getNotification() {
return build();
}
官方给出的也是用bulid()方法取代它,但是直接用build()方法的话我的Api跟它有问题,直接调用废弃的getNotification()方法就可以了
另外Notification 还有很多其他常用属性,比如震动,响铃,控制手机前置的LED灯(这里需要在 AndroidManifest.xml中设置权限:
<uses-permission android:name="android.permission.VIBRATE" /> ):
● 震动:
long[] vibrates = new Long[]{0, 1000, 1000, 1000,...}; // 第零个值表示手机静止的时长,第一个值表示手机震动的时长,
// 第二个值表示手机静止的时长,依次类推,单位是毫秒
notification.vibrate = vibrates;
● 响铃:
Uri soundUri = Uri.fomrFile(new File("/system/media/audio/ringtones/Basic_tone_ogg")); // 这个是手机自带的音频文件
notification.sound = soundUri;
● 控制 LED 灯
// 以绿光一闪一闪的效果
notification.ledARGB = Color.GREEN;
notification.ledOnMS = 1000;
notification.ledOffMS = 1000;
notification.flags = Notification.FLAG_SHOW_LIGHTS;
// 系统默认,它会自己去判断应该放什么铃声
notification.defaults = Notification.DEFAULT_ALL;
一、先来看旧版,Api 11 之前的用法:
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.mipmap.ic_launcher, "This is bitch.", System.currentTimeMillis());
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, "This is ContentTitle","This is ContentText", pi);
manager.notify(1, notification);
manager.cancel(1); // 清除通知栏上的内容,这里的 1 是通知的Id
二、Api 11之后的用法:
NotificationManager manager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
PendingIntent contentIntent = PendingIntent.getActivities(MainActivity.this, 0,
new Intent[]{new Intent(MainActivity.this, MainActivity.class)}, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.mipmap.ic_launcher) //设置状态栏里面的图标(小图标)
// .setLargeIcon(BitmapFactory.decodeResource(resource, R.mipmap.ic_launcher)) //下拉下拉列表里面的图标(大图标)
.setTicker("This is bitch.") //设置状态栏的显示的信息
.setWhen(System.currentTimeMillis()) //设置时间发生时间
.setAutoCancel(true) //设置可以清除
.setContentTitle("This is ContentTitle") //设置下拉列表里的标题
.setContentText("This is ContentText"); //设置上下文内容
Notification notification = builder.getNotification();
manager.notify(1, notification);
Notification notification = builder.geNotification(); // 这里的方法其实已经被废弃了,它里面的源代码就是如下这样
/**
* @deprecated Use {@link #build()} instead.
*/
@Deprecated
public Notification getNotification() {
return build();
}
官方给出的也是用bulid()方法取代它,但是直接用build()方法的话我的Api跟它有问题,直接调用废弃的getNotification()方法就可以了
另外Notification 还有很多其他常用属性,比如震动,响铃,控制手机前置的LED灯(这里需要在 AndroidManifest.xml中设置权限:
<uses-permission android:name="android.permission.VIBRATE" /> ):
● 震动:
long[] vibrates = new Long[]{0, 1000, 1000, 1000,...}; // 第零个值表示手机静止的时长,第一个值表示手机震动的时长,
// 第二个值表示手机静止的时长,依次类推,单位是毫秒
notification.vibrate = vibrates;
● 响铃:
Uri soundUri = Uri.fomrFile(new File("/system/media/audio/ringtones/Basic_tone_ogg")); // 这个是手机自带的音频文件
notification.sound = soundUri;
● 控制 LED 灯
// 以绿光一闪一闪的效果
notification.ledARGB = Color.GREEN;
notification.ledOnMS = 1000;
notification.ledOffMS = 1000;
notification.flags = Notification.FLAG_SHOW_LIGHTS;
// 系统默认,它会自己去判断应该放什么铃声
notification.defaults = Notification.DEFAULT_ALL;