Running a Service in the Foreground
来源:互联网 发布:语音识别 python 编辑:程序博客网 时间:2024/05/21 19:30
A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory.A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, whichmeans that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.
For example, a music player that plays music from a service should be set to run in the foreground, because the user is explicitly aware of its operation.The notification in the status bar might indicate the current song and allow the user to launch an activity to interact with the music player.
To request that your service run in the foreground, call startForeground()
. This method takes two parameters: an integer that uniquely identifies the notification and the Notification
for the status bar. For example:
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, notification);
To remove the service from the foreground, call stopForeground()
. This method takes a boolean, indicating whether to remove the status bar notification as well.This method does not stop the service. However, if you stop the service while it's still running in the foreground, then the notification is also removed.
Note: The methods startForeground()
and stopForeground()
wereintroduced in Android 2.0 (API Level 5). In order to run your service in the foreground on older versions of the platform, you must use the previoussetForeground()
method—see the startForeground()
documentation for information about how to provide backward compatibility.
For more information about notifications, see Creating Status Bar Notifications.
为什么是 foreground ? 默认启动的 Service 是被标记为 background,当前运行的 Activity 一般被标记为 foreground,也就是说你给 Service 设置了 foreground 那么他就和正在运行的 Activity 类似优先级得到了一定的提高。当让这并不能保证你得 Service 永远不被杀掉,只是提高了他的优先级。
- Running a Service in the Foreground
- Running as a foreground service(前台进程)
- Running in a Background Service 之Sending Work Requests to the Background Service
- Running in a Background Service 之Creating a Background Service
- Running in a Background Service 之Reporting Work Status
- Launching Tasks in the Foreground and Background
- Cloning a running Virtual Machine using the Service Console
- bring (force) a Window to the foreground?
- Presenting a local notification immediately while running in the background
- Interacting with the User in a Service
- hide a service in the service tab隐藏服务
- 0901-APP-Service-foreground
- Loading Data in the Background 之Running a Query with a CursorLoader
- Hadoop Platform as a Service in the Cloud
- A Smple JAX-WS Service in the CXF Distribution
- Error: 'The service did not respond in a timely fashion'
- the vmware authorization service is not running
- The VMware Authorization Service is not running
- 一起学android之对资源图片进行比例缩放 (27)
- Linux经典命令
- 结构体字节对齐和位域对齐——VC、gcc
- DrawerLayout 关闭手势滑动
- Servlet,Filter,Listener,Interceptor
- Running a Service in the Foreground
- 学习C/C++中static关键字
- d3,圆弧 arc,饼图
- 返回一个字符串中重复出现的最长字串的长度及其开始字符
- CLOCK_MONOTONIC and CLOCK_REALTIME
- Rotate List
- Win8.1下COCOS2D-X 3.4环境搭建
- C# 中如何编写和调用自己的DLL
- hdu 1348 wall (计算几何,凸包)