通知栏的使用
2016-03-01 15:36
337 查看
需要弄清楚的点
Remoteview作用,使用方法PendingIntent和Intent的区别
Notication和广播BroadCastReceiver组件差异
1 目前掌握的是RmeoteView在自定义通知的时候用来设置自定义通知的布局资源。
2 Intent即时启动,随着所在Activity的消失而消失。Intent一般用作在Activity,Service,BroadCastReceiver之间传递数据。
PendingIntent是Intent的包装,通常通过getActivity,getService,getBroadCast来实例化。当前Activity不能发生启动它所包含的Intent,只有在外部执行PendingIntent时调用Intent。PendingIntent拥有当前App的Context,既是当前App不存在了,也能通过其包含的Context执行Intent。
3 BroadCastReceiver组件没有提供可视化界面显示广播消息,而Noticification和Notication Manager的组合可显示消息的内容及图标、震动等信息到状态栏。
实现步骤:
1 获得服务NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
2 实例化Notification
Notification n = new Notification(R.drawable.new_app_icon, title, when);
when值为要显示的时间,如果不想显示时间可以直接设置0
单独设置进行实例化
Notification n = new Notification();
n.icon = R.drawable.ic_launcher; n.tickerText = "通知"; //不设置没有时间显示 n.when = System.currentTimeMillis(); n.vibrate = new long[] { 5000, 2000, 1000, 10000 };
vibrate参数:延时,震动,暂停,震动
Uri url = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"iloveu.mp3"); n.sound = url;
设置自定义的声音
flag设置使其不能被删除掉,只有点击才能从通知栏删除
n.flags = Notification.FLAG_SHOW_LIGHTS|Notification.FLAG_NO_CLEAR;
通知一直进行,如果是音乐那么一直播放一直到用户响应
notification.flags |= Notification.FLAG_INSISTENT;
3 设置事件消息
PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,MainActivity.class), 0); //设置事件信息 n.setLatestEventInfo(this, null, "内容", mPendingIntent);
setLastEventInfo参数类型:context,title,content,pendingintent
4 发出通知
mNotificationManager.notify();
直接用,报错java.lang.IllegalMonitorStateException: object not locked by thread before notify()
发出id为1的通知n,id为了区分多个通知,单独发出
mNotificationManager.notify(1,n);
5 取消通知
//取消指定id=2的通知 mNotificationManager.cancel(2);
#
拓展实现类似播放器控制播放上一首,下一首
设置RemoteViews布局为播放控制面板,监听播放控制事件,事件发生后发送广播RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notice); Intent frontSong = new Intent("frontSong"); Intent nextSong = new Intent("nextSong"); PendingIntent leftmenuIntent = PendingIntent.getBroadcast(this, 1, frontSong, 1); PendingIntent rightmenuIntent = PendingIntent.getBroadcast(this, 2, nextSong, 2); remoteViews.setOnClickPendingIntent(R.id.left_menu, leftmenuIntent); remoteViews.setOnClickPendingIntent(R.id.right_menu, rightmenuIntent); n.contentView = remoteViews;
广播接收器
public class PlayReciver extends BroadcastReceiver { @Override public void onReceive(Context arg0, Intent arg1) { // TODO Auto-generated method stub //接到广播,播放下一条或上一条 if(arg1.getAction()=="nextSong"){ System.out.println("____nextSong"); } if(arg1.getAction()=="frontSong"){ System.out.println("____frontSong"); } } }
不要忘记加注册还有拦截action的声明
<receiver android:name=".PlayReciver" > <intent-filter> <action android:name="frontSong" /> <action android:name="nextSong" > </action> </intent-filter> </receiver>
需要注意的地方
6/3/2015 4:42:04 PM相关文章推荐
- node.js源码安装
- android View绘制源码分析
- ASP.NET MVC 中实现View与Controller分离
- bzoj 1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 筛法
- 多线程中线程参数的传递,不要使用局部变量
- python 使用poplib获取邮件
- 如何更改iOS上的UISearchBar组件的内部背景颜色
- Hive join操作小结
- 23.Instance Objects
- 关于Redis中的list常见的操作
- java中将汉字转换成16进制
- SQL语句分组排序,多表关联排序
- PHP Ajax 跨域问题最佳解决方案
- 经常在eclipse中导入web项目时,出现转不了项目类型的问题,导入后就是一个java项目。
- 正则表达式基本语法
- 沉浸式状态栏与界面adjustResize之间的bug解决方案
- 这个功能使用说明是每次app更新或者第一次安装都需要显示的
- 自定义控件View(一)__实现宽高比一致的ImageView
- 15分钟学会使用Git和远程代码库
- 方法和变量在继承时的覆盖和隐藏问题