android developer tiny share-20161108
来源:互联网 发布:js数组前面添加元素 编辑:程序博客网 时间:2024/05/20 17:27
今天继续讲android的Service,讲前台服务。前台服务优先级很高,即便内存不足时,也不会考虑回收它。但是,前台服务必须在状态栏中提示用户。这种前台服务主要用于像播放器等功能。
下面是android developer官方文档的讲解:
在前台运行服务
前台服务被认为是用户主动意识到的一种服务,因此在内存不足时,系统也不会考虑将其终止。 前台服务必须为状态栏提供通知,放在“正在进行”标题下方,这意味着除非服务停止或从前台移除,否则不能清除通知。
例如,应该将通过服务播放音乐的音乐播放器设置为在前台运行,这是因为用户明确意识到其操作。 状态栏中的通知可能表示正在播放的歌曲,并允许用户启动 Activity 来与音乐播放器进行交互。
要请求让服务运行于前台,请调用 startForeground()。此方法采用两个参数:唯一标识通知的整型数和状态栏的 Notification。例如:
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text), System.currentTimeMillis());Intent notificationIntent = new Intent(this, ExampleActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);notification.setLatestEventInfo(this, getText(R.string.notification_title), getText(R.string.notification_message), pendingIntent);startForeground(ONGOING_NOTIFICATION_ID, notification);
注意:提供给 startForeground() 的整型 ID 不得为 0。
要从前台移除服务,请调用 stopForeground()。此方法采用一个布尔值,指示是否也移除状态栏通知。 此方法不会停止服务。 但是,如果您在服务正在前台运行时将其停止,则通知也会被移除。
如需了解有关通知的详细信息,请参阅创建状态栏通知。
0 0
- android developer tiny share-20161108
- android developer tiny share-20160627
- android developer tiny share-20160628
- android developer tiny share-20160629
- android developer tiny share-20160630
- android developer tiny share-20160701
- android developer tiny share-20160704
- android developer tiny share-20160705
- android developer tiny share-20160706
- android developer tiny share-20160707
- android developer tiny share-20160708
- android developer tiny share-20160711
- android developer tiny share-20160712
- android developer tiny share-20160713
- android developer tiny share-20160714
- android developer tiny share-20160715
- android developer tiny share-20160718
- android developer tiny share-20160719
- centos6.5hadoop2.7部署和简单例子
- 路帮网的加油站开放接口
- Java中的浮点数比较 == equals 和 compare
- 配置Tomcat服务器
- Spark History Server配置使用
- android developer tiny share-20161108
- Android实用的正则表达式工具
- 使用iText导出word和pdf(经典)
- Mysql中的递归层次查询(父子查询)
- 304. Range Sum Query 2D - Immutable 等题
- 【OpenGL】鼠标触发事件
- 【UML学习】构件图和部署图
- java设计模式之享元模式
- MVN学习1_Centos6.6安装Maven