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.
最新文章
- NGINX、PHP-FPM开机自动启动
- PHP的$_SERVER['PHP_SELF']造成的XSS漏洞攻击及其解决方案
- SVN迁移到Git的过程(+ 一些技巧
- Python标准库09 当前进程信息 (os包)
- 深入理解jQuery中live与bind方法的区别
- opencv中的Bayes分类器应用实例
- Java之网络编程笔记
- Cocos2d-x——pthread的使用注意事项
- 优雅的让Fragment监听返回键
- Learning Docker--chapter 1
- 使用Vitamio打造自己的Android万能播放器(7)——在线播放(下载视频)
- PHP-微信公众平台开发-接收用户输入消息类型并响应
- hdu_3001_Travelling(状压DP)
- [Micropython]TPYBoard v10x拼插编程实验 点亮心形点阵
- java 实现WebService
- bootstrap模态对话框(最简单)
- Erdos
- python写测试接口
- 工作队列.py
- sql语句如何将多个空格字符替换成一个空格字符