android官方文档---应用程序基础---服务的理解(app Component---services)
2013-07-03 18:53
513 查看
********************************
一般来说有创建自己的Service有两种方法, 继承Service或者IntentService
Traditionally, there are two classes you can extend to create a started service:
*********
Once installed on a device, each Android application lives in its own security sandbox:
The Android operating system is a multi-user Linux system in which each application is a different user. 多用户的Linux系统
By default, the system assigns each application a unique Linux user ID (the ID is used only by the system and is unknown to the application). The system sets permissions for all the files in an application so that only the user ID assigned to that application can access them.每个应用程序就是一个user
Each process has its own virtual machine (VM), so an application's code runs in isolation from other applications.每个进程有自己的虚拟机,所以与其他应用是隔离的.
By default, every application runs in its own Linux process. Android starts the process when any of the application's components need to be executed, then shuts down the process when it's no longer needed or when the system must recover memory for other applications. 应用中有任何需要执行的请求则安卓开始这个进程.当进程不再需要或者内存不足时会结束这进程.
继承IntentService只需要一个Constructor和onHandleIntent()方法;
Creates a work queue that passes one intent at a time to your
**********
In this way, the Android system implements the principle of least privilege. That is, each application, by default, has access only to the components that it requires to do its work and no more. This creates a very secure environment in which an application cannot access parts of the system for which it is not given permission.
安卓执行的principle of least privilege,最小特权,每个app只有他require的那些特权
**********
The other component type, content provider, is not activated by intents. Rather, it is activated when targeted by a request from a
content provider不是intent启动的,而是通过ContentResolver.
Connect to an application service, creating it if needed. This defines a dependency between your application and the service. The given conn will receive the service object when it is created and be told if it dies and restarts. The service will be considered required by the system only for as long as the calling context exists. For example, if this Context is an Activity that is stopped, the service will not be required to continue running until the Activity is resumed.
This function will throw
Note: this method can not be called from a
Parameters
***********************
You can start an activity (or give it something new to do) by passing an
You can start a service (or give new instructions to an ongoing service) by passing an
You can initiate a broadcast by passing an
You can perform a query to a content provider by calling
启动component的几种基础方法.
*********************************
A service can essentially take two forms:
StartedA service is "started" when an application component (such as an activity) starts it by calling
一般来说有创建自己的Service有两种方法, 继承Service或者IntentService
Traditionally, there are two classes you can extend to create a started service:
ServiceThis is the base class for all services. When you extend this class, it's important that you create a new thread in which to do all the service's work, because the service uses your application's main thread, by default, which could slow the performance of any activity your application is running.
IntentServiceThis is a subclass of
Servicethat uses a worker thread to handle all start requests, one at a time. This is the best option if you don't require that your service handle multiple requests simultaneously. All you need to do is implement
onHandleIntent(), which receives the intent for each start request so you can do the background work.
*********
Once installed on a device, each Android application lives in its own security sandbox:
The Android operating system is a multi-user Linux system in which each application is a different user. 多用户的Linux系统
By default, the system assigns each application a unique Linux user ID (the ID is used only by the system and is unknown to the application). The system sets permissions for all the files in an application so that only the user ID assigned to that application can access them.每个应用程序就是一个user
Each process has its own virtual machine (VM), so an application's code runs in isolation from other applications.每个进程有自己的虚拟机,所以与其他应用是隔离的.
By default, every application runs in its own Linux process. Android starts the process when any of the application's components need to be executed, then shuts down the process when it's no longer needed or when the system must recover memory for other applications. 应用中有任何需要执行的请求则安卓开始这个进程.当进程不再需要或者内存不足时会结束这进程.
继承IntentService只需要一个Constructor和onHandleIntent()方法;
Creates a work queue that passes one intent at a time to your
onHandleIntent()implementation, so you never have to worry about multi-threading.
**********
In this way, the Android system implements the principle of least privilege. That is, each application, by default, has access only to the components that it requires to do its work and no more. This creates a very secure environment in which an application cannot access parts of the system for which it is not given permission.
安卓执行的principle of least privilege,最小特权,每个app只有他require的那些特权
**********
The other component type, content provider, is not activated by intents. Rather, it is activated when targeted by a request from a
ContentResolver. The content resolver handles all direct transactions with the content provider so that the component that's performing transactions with the provider doesn't need to and instead calls methods on the
ContentResolverobject. This leaves a layer of abstraction between the content provider and the component requesting information (for security).
content provider不是intent启动的,而是通过ContentResolver.
************
bindService这个方法需要再理解一次public abstract boolean bindService (Intent service, ServiceConnection conn, int flags)
Added in API level 1Connect to an application service, creating it if needed. This defines a dependency between your application and the service. The given conn will receive the service object when it is created and be told if it dies and restarts. The service will be considered required by the system only for as long as the calling context exists. For example, if this Context is an Activity that is stopped, the service will not be required to continue running until the Activity is resumed.
This function will throw
SecurityExceptionif you do not have permission to bind to the given service.
Note: this method can not be called from a
BroadcastReceivercomponent. A pattern you can use to communicate from a BroadcastReceiver to a Service is to call
startService(Intent)with the arguments containing the command to be sent, with the service calling its
stopSelf(int)method when done executing that command. See the API demo App/Service/Service Start Arguments Controller for an illustration of this. It is okay, however, to use this method from a BroadcastReceiver that has been registered with
registerReceiver(BroadcastReceiver, IntentFilter), since the lifetime of this BroadcastReceiver is tied to another object (the one that registered it).
Parameters
service | Identifies the service to connect to. The Intent may specify either an explicit component name, or a logical description (action, category, etc) to match an IntentFilterpublished by a service. |
---|---|
conn | Receives information as the service is started and stopped. This must be a valid ServiceConnection object; it must not be null. |
flags | Operation options for the binding. May be 0, BIND_AUTO_CREATE, BIND_DEBUG_UNBIND, BIND_NOT_FOREGROUND, BIND_ABOVE_CLIENT, BIND_ALLOW_OOM_MANAGEMENT, or BIND_WAIVE_PRIORITY. |
You can start an activity (or give it something new to do) by passing an
Intentto
startActivity()or
startActivityForResult()(when you want the activity to return a result).
You can start a service (or give new instructions to an ongoing service) by passing an
Intentto
startService(). Or you can bind to the service by passing an
Intentto
bindService().
You can initiate a broadcast by passing an
Intentto methods like
sendBroadcast(),
sendOrderedBroadcast(), or
sendStickyBroadcast().
You can perform a query to a content provider by calling
, java.lang.String, java.lang.String[], java.lang.String)]query()on a
ContentResolver.
启动component的几种基础方法.
*********************************
A service can essentially take two forms:
StartedA service is "started" when an application component (such as an activity) starts it by calling
startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.BoundA service is "bound" when an application component binds to it by calling
bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.Service有两种形式,一种被start, 这种服务是独立的运行于后台. 一般来说用于执行Single Operation. 执行完就结束了自己的生命周期另一种是被绑定的(bound),这种可以send,get result, even across processes. Bound的服务当其上的其他component结束后自己也结束. One service can be bound by multiple components.
相关文章推荐
- Android官方文档-Services(服务)
- Android HIDL 官方文档(四)—— 服务与数据转换(Services & Data Transfer)
- Android 触摸手势基础 官方文档概览
- Android 触摸手势基础 官方文档概览
- Android应用程序签名(官方文档中文版)
- Android官方开发文档Training系列课程中文版:APP的内存管理
- android services启动方式 官方文档翻译
- Android: AppWidget开发官方文档
- Android官方开发文档Training系列课程中文版:与其它APP交互之允许其它APP启动你的Activity
- 官方文档:Android应用程序运行的性能设计
- Android Studio官方文档之添加URL和App索引支持
- Android官方文档翻译 四 1.2Running Your App
- Android 触摸手势基础 官方文档概览2
- Android Web App官方文档翻译第一章:概览
- Android官方文档之App Resources(下)
- Android应用程序签名(官方文档中文版)
- App开发日报 2015-05-20 如何仅凭官方文档零基础学习Swift开发
- 【android官方文档】与其他App交互
- Android 触摸手势基础 官方文档概览