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.

最新文章

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

热门文章

  1. 配置activeMQ
  2. Office WORD WPS如何设置PPT播放全屏
  3. MVC在View中页面跳转
  4. 从PRISM开始学WPF(一)WPF?
  5. 块状元素的text-align对齐属性
  6. 排队理论之性能分析 - Little Law & Utilization Law
  7. Infrastructure for container projects.
  8. PHP 7.2 RC3 on CentOS/RHEL 7.3 via Yum
  9. Ant 打包 问题
  10. [NOI2018] 归程 可持久化并查集