Wednesday, April 17, 2013

Starting and Stopping Executable from Android Framework.

1)Add the service in the ini.rc of android source and build it.

The Path of init.rc in the JB source it will be
android_src/device/...

service testservice /system/bin/testservice
class main
disabled
oneshot

2) Starting Service:

Framework to Native code communication is done in JNI, in JNI method add this
code to start service:

 char testservice_status[PROPERTY_VALUE_MAX] = {'\0'};
property_set("ctl.start", "testservice');
    sched_yield();
    usleep(100000);
    if (property_get( "init.svc.testservice", testservice_status, NULL)) {
             if (strcmp(testservice_status, "running") == 0)
                 return 0;
         }   
    usleep(100000);


3)Stopping Service:

/* Check whether service already stopped */
           if (property_get(init.svc.testservice, testservice_status, NULL)
               && strcmp(testservice_status, "stopped") == 0) {
                       return status;
           }

           property_set("ctl.stop", "testservice");
           sched_yield();

           while (count-- > 0) {
               if (property_get("init.svc.testservice", testservice_status, NULL)) {
                         if (strcmp(testservice_status, "stopped") == 0)
                      return status;
               }
               usleep(100000);
           }



Without compiling the code Start and Stop service can be verfied by setprop and getprop functionality through adb shell.