Android开发艺术探索<Notification使用>
2016-11-21 10:46
579 查看
Notification用来发送一个通知,作者在文中有一段代码是关于Notification的基本使用,就是
各位注意一下红色字体标出的代码,用来设置Notification的Title和Content,还有一个关于点击之后的意图。但是在Notification整个类中都没有找到setLastEventInfo这个方法,猜想应该是API升级的原因(现在用的是24版本的)。然后自己找到API21的一个android.jar包,打开> Notification这个类,果然找到了这个方法:
很明显,这个方法被废了,不再支持。
那么一个Notification的Title和Content的文字怎么设置呢。虽然Notification对象中并没有提供给我们一些字段或者set方法去设置。但是Android还提供了一个Notification下的内部类:>Builder,链式编程的套路。
但是这个方法在API版本达到16以上才能用,版本兼容怎么办?答:V4包下还有一个类
大意就是:Android不会置我于不顾的,放心的用吧。那么一个Notification的使用Demo就能写成如下:
然后要注意的是:setSmallIcon这个方法,不设置的话通知不会弹出.
最后再来点:
注意cancel()方法的ID,我们在notify()方法中的第一个参数就是一个ID,这里要保持一致。
OK,了解更多请参考:
郭神的Blog:http://blog.csdn.net/sinyu890807/article/details/50945228
还有他的《第一行代码》
`Notification notification = new Notification(); notification.icon = R.drawable.ic_launcher;//通知的图标 notification.tickerText = "hello world";//提示用户的通知文案 notification.when = System.currentTimeMillis(); notification.flags = Notification.FLAG_AUTO_CANCEL; Intent intent = new Intent(this, DemoActivity_2.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);//点击之后所做的事情。 notification.setLatestEventInfo(this, "chapter_5", "this is notification.", pendingIntent); NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(sId, notification);`
各位注意一下红色字体标出的代码,用来设置Notification的Title和Content,还有一个关于点击之后的意图。但是在Notification整个类中都没有找到setLastEventInfo这个方法,猜想应该是API升级的原因(现在用的是24版本的)。然后自己找到API21的一个android.jar包,打开> Notification这个类,果然找到了这个方法:
/** @deprecated */ @Deprecated public void setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) { throw new RuntimeException("Stub!"); }
很明显,这个方法被废了,不再支持。
那么一个Notification的Title和Content的文字怎么设置呢。虽然Notification对象中并没有提供给我们一些字段或者set方法去设置。但是Android还提供了一个Notification下的内部类:>Builder,链式编程的套路。
`Notification.Builder builder = new Notification.Builder(this); builder.setContentText("Content"); builder.setContentTitle("Title"); builder.build();`
但是这个方法在API版本达到16以上才能用,版本兼容怎么办?答:V4包下还有一个类
/** * Helper for accessing features in {@link android.app.Notification} * introduced after API level 4 in a backwards compatible fashion. */ public class NotificationCompat {
大意就是:Android不会置我于不顾的,放心的用吧。那么一个Notification的使用Demo就能写成如下:
`Notification notify = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { notify = new Notification.Builder(this) .setContentTitle("Title")// .setTicker("ticker")//出来闪一下那个 .setContentText("Content") .setSmallIcon(R.drawable.ic_menu_allfriends) .setLargeIcon(bitmap) .setContentIntent(pendingIntent) .build(); } else { NotificationCompat.Builder builder = new NotificationCompat.Builder(this); notify = builder.setContentTitle("TITLE") .setTicker("ticker") .setContentText("Content") .setContentTitle("Title") .setWhen(System.currentTimeMillis()) //这是状态栏显示的图标,5.0后谷歌要求这个图标只能用alpha通道绘制,不能有RGB颜色 .setSmallIcon(R.mipmap.mn) //这是通知栏显示的图标,5.0以上会显示,4.4手机只会显示应用的ICON(可有可无) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.a)) .setColor(Color.BLUE)//更改小图标内的颜色 .build(); } NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(1, notify);`
然后要注意的是:setSmallIcon这个方法,不设置的话通知不会弹出.
最后再来点:
1.丰富你的通知:
`notify.vibrate = new long[]{0, 1000, 1000, 1000};//通知到来的时候打开振动,记得添加权限,手机别设置成静音 notify.ledARGB = Color.BLUE;//在锁屏的时候才有效果 notify.ledOnMS = 2000; notify.ledOffMS = 1000; notify.flags = Notification.FLAG_SHOW_LIGHTS;`
2.取消通知
在你点击通知进入某个Activity的时候在该Activity中增加代码:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_uichange); //通知栏点击进入到此界面,取消相应的通知 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.cancel(1);//这里的ID是之前写好的 }
注意cancel()方法的ID,我们在notify()方法中的第一个参数就是一个ID,这里要保持一致。
OK,了解更多请参考:
郭神的Blog:http://blog.csdn.net/sinyu890807/article/details/50945228
还有他的《第一行代码》
相关文章推荐
- Android开发艺术探索<Drawable系列之五ScaleDrawable >
- Android开发艺术探索<View的点击事件触发过程>
- <<Android开发艺术探索>>读书笔记--Activity LaunchMode
- <<Android 开发艺术探索>> 第一章 Actiivty的生命周期和启动模式
- Android开发艺术探索<Drawable系列之二ShapeDrawable>
- Android开发艺术探索<Android的线程池>
- Android开发艺术探索<Android的序列化>
- <<Android开发艺术探索>>读书笔记--IntentFilter
- Android开发艺术探索<Drawable系列之四StateListDrawable >
- Android开发艺术探索<Drawable系列之六ClipDrawabe >
- Android开发艺术探索<Drawable系列之一BitmapDrawable>
- 【Android开发艺术探索】IPC机制(四)-使用AIDL进行跨进程通信
- 读 << Android开发之Theme、Style探索及源码浅析 >> 一文记录下
- 【Android开发学习45】使用google语音识别引擎(Google Speech API)<一>
- 【Android开发学习45】使用google语音识别引擎(Google Speech API)<一>
- 【Android开发艺术探索】IPC机制(三)-使用Messenger进行跨进程通信
- android_应用开发之(使用<include>标签重用布局)
- 搭建gVim+eclim开发android应用程序<原创>
- <备忘10001>Android开发小贴士1
- Android开发工具——ADB(Android Debug Bridge) <一>概览