• android官方文档---应用程序基础---服务的理解(app Component---services)


    ********************************

    一般来说有创建自己的Service有两种方法, 继承Service或者IntentService

    Traditionally, there are two classes you can extend to create a started service:

    Service
    This 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.
    IntentService
    This is a subclass of Service that 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 ContentResolver object. 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 1

    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 SecurityException if you do not have permission to bind to the given service.

    Note: this method can not be called from a BroadcastReceiver component. 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 IntentFilter published 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_CREATEBIND_DEBUG_UNBINDBIND_NOT_FOREGROUNDBIND_ABOVE_CLIENT,BIND_ALLOW_OOM_MANAGEMENT, or BIND_WAIVE_PRIORITY.
     

    ***********************

      启动component的几种基础方法.

    *********************************

    A service can essentially take two forms:

    Started
    A 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.
    Bound
    A 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.
  • 相关阅读:
    python高级编程
    django笔记
    sublime ide
    python3 django mysql
    python win
    linux时区设置
    在实际应用中如何实现切圆角的功能
    display和visiblity在应用中使用哪个好
    看懂UML类图和时序图
    解决Xcode7.2真机调试出现:The account “” has no team with ID “”
  • 原文地址:https://www.cnblogs.com/linxiaojiang/p/3170080.html
Copyright © 2020-2023  润新知