android4.0平台service程序分析

来源:互联网 发布:淘宝店铺背景图片尺寸 编辑:程序博客网 时间:2024/06/06 10:41

service程序源代码路径frameworks/base/cmds/service/service.cpp

int main()

{

    // 获得ServiceManager服务的接口sm, 通过这个接口客户获得其他服务信息

    sp<IServiceManager> sm = defaultServiceManager();

    // 使stdout清空,立刻输出所有在缓冲区中的内容

    fflush(stdout);

    if (sm == NULL) { //获取ServiceManager服务接口失败, 直接返回
        aerr << "service: Unable to get default service manager!" << endl;
        return 20;
    }

    bool wantsUsage = false;// 是否需要显示帮助信息, false不打印帮助信息, true打印帮助信息

    int result = 0;

    while (1) {

         // getopt()函数的原型为getopt(int argc,char *const argv[],const char *optstring)。

        // optstring是一段自己规定的选项串,例如本例中的"h?",表示可以有,-h,-?这几个参数。

         int ic = getopt(argc, argv, "h?");

         if (ic < 0)

            break;

         switch (ic) {

         case 'h':

         case '?':

            wantsUsage = true;

            break;

        default:

           aerr << "service: Unknown option -" << ic << endl;

           wantsUsage = true;

           result = 10;

          break;

        }

        // 全域变量optind指示下一个要读取的参数在argv中的位置。

       if (optind >= argc) {

           wantsUsage = true;

       }  else if (!wantsUsage) {

           if (strcmp(argv[optind], "check") == 0) { // service check activity

                optind++; // 下一个需要读取的位置加1

                if (optind < argc) { // 还有读取参数activity

                     // 在serviceManager中查找是否有activityManagerService服务, 

                     // 这个服务注册到ServiceManager的时候名字为"activity"

                     sp<IBinder> service = sm->checkService(String16(argv[optind]));

                     aout << "Service " << argv[optind] <<(service == NULL ? ": not found" : ": found") << endl;

                } else {

                      aerr << "service: No service specified for check" << endl;

                      wantsUsage = true;

                      result = 10;

                }

           } // endif

          else if (strcmp(argv[optind], "list") == 0) {// 命令为adb shell service list

               // 列出所有在ServiceManager中注册的服务的名称

               Vector<String16> services = sm->listServices();

               aout << "Found " << services.size() << " services:" << endl;

               for (unsigned i = 0; i < services.size(); i++) {

                      String16 name = services[i];

                      sp<IBinder> service = sm->checkService(name); // 根据服务的名称查找对应服务的代理对象

                      aout << i << "\t" << good_old_string(name) 

                              << ": [" << good_old_string(get_interface_name(service)) << "]"

                             << endl;

               }

           }  else if (strcmp(argv[optind], "call") == 0) { // 通过adb shell service call 调用某一个服务

               // 一般格式如下: adb shell servie call window 74 i32 0

               // widdow 服务名称; 74表示函数 i32参数类型为整形, 0 为参数值

              // 不过adb shell service call 命令调用一般使用比较少。

           }

}


一般使用方式为 adb shell service list


0 0
原创粉丝点击