Service官方教程(5)后台服务发送通知、把服务变前台服务。
1.Sending Notifications to the User (发送通知)
Once running, a service can notify the user of events using Toast Notifications or Status Bar Notifications.
A toast notification is a message that appears on the surface of the current window for a moment then disappears, while a status bar notification provides an icon in the status bar with a message, which the user can select in order to take an action (such as start an activity).
Usually, a status bar notification is the best technique when some background work has completed (such as a file completed downloading) and the user can now act on it. When the user selects the notification from the expanded view, the notification can start an activity (such as to view the downloaded file).
See the Toast Notifications or Status Bar Notifications developer guides for more information.
2.Running a Service in the Foreground (前台服务)
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, which means 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:
startForeground()方法可以让service变前台。
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, , notificationIntent, );
notification.setLatestEventInfo(this, getText(R.string.notification_title),
getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);
Caution: The integer ID you give to startForeground() must not be 0.
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.
For more information about notifications, see Creating Status Bar Notifications.
Service官方教程(5)后台服务发送通知、把服务变前台服务。的更多相关文章
- Service官方教程(2)*IntentService与Service示例、onStartCommand()3个返回值的含义。
1.Creating a Started Service A started service is one that another component starts by calling start ...
- Service官方教程(6)Bound Services主要用来实现通信服务,以及3种实现通信的方案简介。
1.Bound Services A bound service is the server in a client-server interface. A bound service allows ...
- Service官方教程(9)绑定服务时的注意事项
Binding to a Service Application components (clients) can bind to a service by calling bindService() ...
- Service官方教程(8)Bound Service示例之2-跨进程使用Messenger
Compared to AIDL When you need to perform IPC, using a Messenger for your interface is simpler than ...
- Service官方教程(11)Bound Service示例之2-AIDL 定义跨进程接口并通信
Android Interface Definition Language (AIDL) 1.In this document Defining an AIDL Interface Create th ...
- Service官方教程(1)Started与Bound的区别、要实现的函数、声明service
Services 简介和分类 A Service is an application component that can perform long-running operations in the ...
- Service官方教程(10)Bound Service的生命周期函数
Managing the Lifecycle of a Bound Service When a service is unbound from all clients, the Android sy ...
- Service官方教程(7)Bound Service示例之1-同进程下直接继承Service
Extending the Binder class If your service is used only by the local application and does not need t ...
- Service官方教程(4)两种Service的生命周期函数
Managing the Lifecycle of a Service The lifecycle of a service is much simpler than that of an activ ...
随机推荐
- 解决Win7 64bit + VS2013 使用opencv时出现提“应用程序无法正常启动(0xc000007b)”错误
应用程序无法正常启动(0xc000007b) 记得以前也遇到过这样的问题:网上的解决方法就是修复什么 今天配置opencv2.4.8+vs2013的时候,发现用老版本的程序是不是都会出现这样的现象啊! ...
- Duplicate property mapping of contactPhone found in
启动的时候报Duplicate property mapping of contactPhone found in com....的错误,是因为在建立实体对象的时候,有字段重复了,有的是继承了父类的字 ...
- Deepin-键盘快捷键
是不是很happy呢? 可以用键盘替代鼠标点点点了! 1.鼠标移到右下角 2.下翻找到"快捷键" 3.自定义一个 4.示例(首先编写个简单的Shell) 程序一般放在/usr/bi ...
- Node 即学即用 笔记 思维导图
Node即学即用 REPL(Read-Evaluate-Print-Loop) console.log .clear .help .exit require('http') ...
- 图像处理之基础---boxfiter
http://blog.sina.com.cn/s/blog_7221228801019yg2.html DSP6000图像位移与变形典型算法 http://blog.csdn.net/anson20 ...
- 【iOS-Tips】-小贴士
[iOS-Tips]-小贴士 1:UIImage的2种加载方式: 方式一:有缓存(图片所占用的内存会一直停留在程序中) //name是图片的文件名 + (UIImage *)imageNamed:(N ...
- 【iOS系列】-文件管理
OC中操作文件,需要使用NSFileManager: 需要使用NSFileManager的创建方式: //单例模式创建对象 NSFileManager * f2 = [NSFileManager de ...
- 2016/04/26 流程 数据库lcdb 四个表 1,用户表users 2,流程表(设定有哪些流程)liucheng 3,流程发起者表(记录谁发起到哪里) 4,流程经过的人员表 flowpath (order排序)
流程: 十一 个页面 1,denglu.php(登录) <!DOCTYPE html> <html lang="en"> <head> ...
- http的session和cookie
1 http session和http请求之间的关系 http协议是无状态的,一次会话服务端需要处理多次http请求,就算是长连接,也是要发送多次请求的,由于http无状态所有每次的请求都是独立的,服 ...
- 大括号对struct进行初始化
1 partial initialization 即所谓的部分初始化. 这个时候,无论该struct变量是static的还是automic的,未显式初始化的成员都会被初始化为默认值.