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)后台服务发送通知、把服务变前台服务。的更多相关文章

  1. Service官方教程(2)*IntentService与Service示例、onStartCommand()3个返回值的含义。

    1.Creating a Started Service A started service is one that another component starts by calling start ...

  2. Service官方教程(6)Bound Services主要用来实现通信服务,以及3种实现通信的方案简介。

    1.Bound Services A bound service is the server in a client-server interface. A bound service allows ...

  3. Service官方教程(9)绑定服务时的注意事项

    Binding to a Service Application components (clients) can bind to a service by calling bindService() ...

  4. Service官方教程(8)Bound Service示例之2-跨进程使用Messenger

    Compared to AIDL When you need to perform IPC, using a Messenger for your interface is simpler than ...

  5. Service官方教程(11)Bound Service示例之2-AIDL 定义跨进程接口并通信

    Android Interface Definition Language (AIDL) 1.In this document Defining an AIDL Interface Create th ...

  6. Service官方教程(1)Started与Bound的区别、要实现的函数、声明service

    Services 简介和分类 A Service is an application component that can perform long-running operations in the ...

  7. Service官方教程(10)Bound Service的生命周期函数

    Managing the Lifecycle of a Bound Service When a service is unbound from all clients, the Android sy ...

  8. 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 ...

  9. Service官方教程(4)两种Service的生命周期函数

    Managing the Lifecycle of a Service The lifecycle of a service is much simpler than that of an activ ...

随机推荐

  1. 《Java设计模式》之訪问者模式

    訪问者模式是对象的行为模式.訪问者模式的目的是封装一些施加于某种数据结构元素之上的操作.一旦这些操作须要改动的话,接受这个操作的数据结构则能够保持不变. 分派的概念 变量被声明时的类型叫做变量的静态类 ...

  2. 初步认识Tensorflow

    不多说,直接上干货! TensorFlow 是一个开源软件库,用于使用数据流图进行数值计算.换句话说,即是构建深度学习模型的最佳方式. Tensorflow的官网 https://www.tensor ...

  3. Python爬虫开发【第1篇】【Scrapy入门】

    Scrapy的安装介绍 Scrapy框架官方网址:http://doc.scrapy.org/en/latest Scrapy中文维护站点:http://scrapy-chs.readthedocs. ...

  4. Android-support-v4源码查看

  5. BZOJ1016 && JSOI2008] 最小生成树计数

    题目链接:id=1016">点击打开链接 裸题 #pragma comment(linker, "/STACK:1024000000,1024000000") #i ...

  6. IDE配置jvm参数

    -------- IntelliJ IDEA 配置参数:-Xms34m -Xmx234m 内存初始化大小,最小和最大值: 测试代码: public class JVMDemoTest { public ...

  7. C#使用 webBrowser 控件总结

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. easyui表格格线错位

    现象:如果easyui表格中存在后渲染的linkbutton,则可能会导致表格固定列的格线与内容列的格线不一致,出现这种情况的原因是在表格的onLoadSuccess事件中渲染linkbutton时, ...

  9. html5--7-33 阶段练习5

    html5--7-33 阶段练习5 总结: 1.JS中可以递归函数 2.js中数组对象array的使用 学习要点 综合运用学过的知识完成三个综合小练习,巩固学过的知识. 阶段小练习5-1:使用递归算法 ...

  10. sqlite支持linq

    A small library to easily access SQLite databases from .NET/Mono/MonoTouch applications In order to ...