Android学习记录02——Broadcast初体验
今天小小的学习了android中的BroadcastReceiver广播接收者部分。
参考课本,通过一个小例子,加深了对broadcast的认识。
在创建Notification时,书上的代码是
Notification notification =new Notification(R.mipmap.ic_launcher,intent.getExtras().getString("content"), System.currentTimeMillis());
notification.setLatestEventInfo(context,intent.getExtras().getString("content"),null,pendingIntent);
但是Android Studio不能识别setLatestEventInfo()这个方法,原因是在高版本中已经放弃这个方法了(无奈)。
然后我通过百度,查到了在高版本的Android Studio中,使用以下方法进行创建Notification,
Notification.Builder notification = new Notification.Builder(context);
notification.setSmallIcon(R.mipmap.ic_launcher); //设置图标为mipmap中的ic_launcher(mipmap中有好几张图片不同大小,自动匹配)
notification.setContentTitle("Broadcast Infomation"); //设置标题
notification.setContentText(intent.getExtras().getString("content")); //从intent中获取广播的内容
notification.setWhen(System.currentTimeMillis()); //发送时间
notification.setContentIntent(pendingIntent); //点击广播可以返回
Notification notification1 = notification.build();
notificationManager.notify(R.layout.activity_main,notification1);
这里使用Notification.Builder来创建Notification,然后对广播进行设置,除了以上几个设置,还有如下几个设置:
notification.setAutoCancel(true); //打开程序后图标消失
notification.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音,震动方式,灯光
等等。
如果该广播只是起到 “通知”的作用,不希望用户点击后有相应的跳转,那么,intent,pendingIntent这几行代码可以不写。
参考文章:http://blog.csdn.net/songyachao/article/details/51245370

浙公网安备 33010602011771号