In the Androird,
service is a none-UI background process that is doing some specific jobs.



  6.1 Example Program : Understanding of Android Service.



  6.2 Classification of Android Services


    * Android Service

      * System Service

        * Java System Service

          * Core Platform Service

          * Hardware Service

        * Native System Service

      * Application Service

        * Local Service

        * Remote Service



  6.3 Android Application Service

    * It's one of the android application components.

      * Usage

        * Service start/end

          : Start/End a service on the background.

        * Remote control through binding

          : If a service client binds to the service, the client can control 

           functions of the service through an interface provided by service 

           while the binding is maintained.

    6.3.1 Separation of Application Service

※ The example of this chapter is in the API demos of Android 2.2.

      * The application service is separated to local service and remote service. 

       A basis of this separation is whether an application and a service that is 

       created by application is in same process or separate.

      * The local service ends when application ends. but the remote service 

       don't end though application ends.

      * The difference between local service and remote service is the binding 

       way for the service control.

        - Local Service

          : Because the client program and service are on the same process, so 

           local service binding needs only reference of local service that is 

           bound by client.

        - Remote Service

          : For using a service, Activity must use IPC mechanism. In this case, 

           binder IPC is used. When it sends or receives between service and 

           activity in the binder IPC communication, it must go through 

           marshalling or unmarshalling, for this process it uses the AIDL.

※ AIDL(Android Interface Definition Language)

  : On the android device, The AIDL is a IDL language for writing codes to 

   communicate between two prosesses. 

   (http://developer.android.com/guide/developing/tools/aidl.html)

A. Local Service

        (1) Try to local binding through bindSercive() API.

          * bindService(Intent, ServiceConnection, int)

            - Intent

              : An intent for local service.

            - ServiceConnection

              : On the service client side, it processes the binding connection

               with service. 

            - int

              : Context.BIND_AUTO_CREATE is a flag that creates a service 

               automatically, if there is not service to bind.

        (2) For the binding processing, android calls the onBind() callback 

           method in service.


          * the onBind() method returns a LocalBinder object that is extended for 

           connecting with activity and itself.

        (3) Android framework calls the onServiceConnected(ComponentName, 

           IBinder) method in the side of service client.

          * mBoundService = ((LocalService.LocalBinder)service).getService()
            - The IBinder argument is returned from onBinder() method.

        (4) Save a reference value of the LocalService into the mBoundService  
  

           member field in activity.

B. Remote Service

        (1) Binding Activity : Requesting to connect with RemoteService


          * Using the bindService() API for the control.

          * The different part with local binding is in manifest file that 

           includes processing about com.example.android.apis.app.ISecondary

           action.

        (2) RemoteService Service : Implementing the Actual Service Method 

           Function and Providing the Binder Object for communicating with 

           Service.

          * Main roles of the onBind() method is for creating the service binder 

           object for processing the binder IPC and returning it.

          * The binder service object is created through the automatically 

           created ISecondary.stub class of ISecondary.java file. And when the 

           binder service object is created, implement the getPid() method(or 

           whatever) as the actual code.

        (3) Binding Activity : Create the Proxy Object for Processing the Service 

           and the Binder IPC.


          * When the onServiceConnected() call-back method of ServiceConnection 

           object is called, the second IBinder typed argument is passed to 

           ISecondary.Stub.asInterface() function. And then created and returned 

           ISecondary.Stub.Proxy proxy service object is saved into 

           mSecondaryService member variable.

          * It's the end of binding process. And after this process, a activity 

           can call methods of RemoteService like it owns them.

        (4) Binding Activity : Through the Using of Proxy Object, Call The 

           Service Proxy Method in the Remote Service.

        (5) Binder IPC : Pass the Binder IPC Data from the Service Proxy Object 

           to Service Binder Object.

        (6) RemoteService Service : Call the Stub Method of RemoteService 

           Service.

※ The Creation Classification of Local Service With Remote Service.

  * The answer is in the manifest file. All of the android services must be 

   represented to elements in the manifest file.

  A. Local Service

    : Only of names of that can be implemented service from class are written 

     in the "android:name" attribute.

  B. Remote Service

    : In addition to local service, remote service is using the 

     "android:process" attribute in the manifest file. If there is not the 

     attribute, that service will perform as a local service.

  6.4 Android System Service

    * Android system services are in the application framework layer and 

     libraries layer.

    6.4.1 Classification of System Service

      (1) Native System Service


        * Representatively, audio flinger service and surface flinger service are 

         exist.

      (2) Java System Service

        A. Core Platform Service

        B. Hardware Service

  6.5 Running the System Service

    * Can use the system service directly with getSystemService()
method. 

     Because, the system service is launched by init process on the booting 

     process.

    * The system service is started by the media server and the system server.

      - The media server process

        : Execute the native services like audio server or media player server.

      - The system server process

        : This process is on the java based, and created first by Zygote.

        : Execute native system services like the surface flinger and etc.

* Creation Process on the Android System Booting

      (1) Start the Media Server Processes.

      (2) Start the Zygote.

      (3) Start the System Server.

      (4) Start the Surface Flinger Services.

      (5) Start the Java System Services.



    6.5.1 Media Server Execution Code Analysis

      (1) Start from Init Process.

        * It makes the native services(Audio Flinger, Media Player Service, 

         Camera Service, Audio Policy Service, etc) started  by
the media server.

      (2) Create and Initialize the Native Service Instance.

        * Do this in the main function of media server.

      (3) Look up the Initializing Code in Each System Services.

        * Using binder IPC.

        * Service provider like system service have to register information to 

         manifest manager.

        * Each initializing codes are shaped of same form. just using new and 

         addService() each services' functions to context manager.

        * defaultServiceManager() function returns an object, service manager is 

         sort of proxy object doing binder communication with the context 

         manager.

        * It has simple mechanism. Just create each instance of service, and 

         register to context manager.



    6.5.2 Analysis of Running Code of Server

      (1) Created by Zygote.

      (2) Load android_servers library.


        * In the main() method of SystemServer class, system_init() JNI function 

         is called by the init1() method. Because, SystemServer is a Java 

         process. So can't call the c++ based custom service.

        * After start a service, call the callStatic() function to call the 

         init2() method.

      (3) Initialize and Register the Java System Service.

        * In the init2() method, Create and run the ServerThread that creates all 

         of the Java system service.

        * Register to ContextManager as like native system services, but use the 

         addService() static method in the ServiceManager class.






Android Service Summary的更多相关文章

  1. android service两种启动方式

    android service的启动方式有以下两种: 1.Context.startService()方式启动,生命周期如下所示,启动时,startService->onCreate()-> ...

  2. 1、Android Studio集成极光推送(Jpush) 报错 java.lang.UnsatisfiedLinkError: cn.jpush.android.service.PushProtoco

    Android studio 集成极光推送(Jpush) (华为手机)报错, E/JPush: [JPushGlobal] Get sdk version fail![获取sdk版本失败!] W/Sy ...

  3. Android Service完全解析,关于服务你所需知道的一切(下)

    转载请注册出处:http://blog.csdn.net/guolin_blog/article/details/9797169 在上一篇文章中,我们学习了Android Service相关的许多重要 ...

  4. Android Service完全解析,关于服务你所需知道的一切(上)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11952435 相信大多数朋友对Service这个名词都不会陌生,没错,一个老练的A ...

  5. android service 的各种用法(IPC、AIDL)

    http://my.oschina.net/mopidick/blog/132325 最近在学android service,感觉终于把service的各种使用场景和用到的技术整理得比较明白了,受益颇 ...

  6. Android service介绍和启动方式

    1.Android service的作用: service通常是用来处理一些耗时操作,或后台执行不提供用户交互界面的操作,例如:下载.播放音乐. 2.Android service的生命周期: ser ...

  7. Android Service初始

    一.Service概念 1.Service是一个应用程序组件 2.Service没有图像化界面 3.Service通常用来处理一些耗时比较长的操作 4.可以使用Service更新ContentProv ...

  8. Android Service与Thread的区别

    Android Service,后台,Android的后台就是指,它的运行是完全不依赖UI的.即使Activity被销毁,或者程序被关闭,只要进程还在,Service就可以继续运行.比如说一些应用程序 ...

  9. Android service binder aidl 关系

    /********************************************************************************** * Android servic ...

随机推荐

  1. meet-in-the-middle 基础算法(优化dfs)

    $meet-in-the-middle$(又称折半搜索.双向搜索)对于$n<=40$的搜索类型题目,一般都可以采用该算法进行优化,很稳很暴力. $meet-in-the-middle$算法的主要 ...

  2. DynamicDataDisplay 双击获取坐标

    近日由于项目需要,学习了DynamicDataDisplay实现动态曲线图,网上的资料基本上够用了,就是双击获得数据点没能找到资料,只好下载了DynamicDataDisplay的源码来学习.总结共享 ...

  3. JS---元素隐藏的不同方式

    元素隐藏的不同方式 dispaly, visibility, opacity, height&border 为0 <!DOCTYPE html> <html lang=&qu ...

  4. tesseract ocr训练 pt验证码

    识别率有问题A大概率识别为n,因此需要训练,这里讲一下 如何训练 参考 java代码里边直接使用tess4j,是对tesseract的封装,但是如果要训练,还是需要在进行安装tesseract-ocr ...

  5. TomCat 启动默认加载项目

    在最后加上这句代码即可:<Context path="" docBase="\项目名称" reloadable="true" cros ...

  6. 提升mysql服务器性能(复制原理与拓扑优化)

    原文:提升mysql服务器性能(复制原理与拓扑优化) 版权声明:皆为本人原创,复制必究 https://blog.csdn.net/m493096871/article/details/9008171 ...

  7. Django多业务模块的写法

    from django.shortcuts import render # Create your views here. from django.shortcuts import HttpRespo ...

  8. 【linux配置】虚拟机配置静态IP地址

    使用VMware配置虚拟机静态IP地址 一.安装好虚拟后在菜单栏选择编辑→ 虚拟网络编辑器,打开虚拟网络编辑器对话框,选择Vmnet8 Net网络连接方式,随意设置子网IP,点击NAT设置页面,查看子 ...

  9. [Vue CLI 3] public 目录没用吗

    在 vue-cli 3 初始化的项目根目录下面:和 src 同级有一个 public 目录 官网的说明如下:https://cli.vuejs.org/zh/guid... 在下列情况下使用: 你需要 ...

  10. 怎么使用mysqlreplicate快速搭建MySQL主从呢?

    用其中的mysqlreplicate工具来快速搭建MySQL主从环境. HE1:192.168.1.248 slave HE3:192.168.1.250 master 实战 Part1:安装mysq ...