Init.rc file to launch C + + program, through JNI call Java implementation

Source: Internet
Author: User


Function: The mobile phone after the boot to write a flag bit.

There is an implementation, that is, after receiving the power-on broadcast, the startup service reads the flag bits in BP (Java code waits for Ril to be ready),
And write this flag bit to the AP (Java code calls C + +).

Problems encountered later: In practice, it was found that receiving a boot broadcast waits longer and requires writing to the AP time ahead.

Solution:
Start the C + + program in the Init.rc file and invoke the Java implementation via JNI.

The refinement code is as follows:
C + + program code:

#define LOG_TAG "Testmain" #define Log_ndebug 0#include <dlfcn.h> #include <cutils/log.h> #include <utils /log.h> #include <jni.h>using namespace android;void wirtepscalivaltokernel (); int main (int argc, char * argv[])    {Alogi ("Start C");    Wirtepscalivaltokernel ();    Alogi ("Start C End"); return 0;}    void Wirtepscalivaltokernel () {javavm* vm;    jnienv* env;    Start a VM Javavminitargs args;    Javavmoption opt;    opt.optionstring = "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";    Args.version = jni_version_1_4;    Args.options = &opt;    Args.noptions = 1;    args.ignoreunrecognized = Jni_false;    void* Libdvm_dso = Dlopen ("libdvm.so", Rtld_now);    Aloge_if (!libdvm_dso, "Ddmconnection:%s", Dlerror ());    void* Libandroid_runtime_dso = Dlopen ("libandroid_runtime.so", Rtld_now);    Aloge_if (!libandroid_runtime_dso, "Ddmconnection:%s", Dlerror ());    if (!libdvm_dso | |!libandroid_runtime_dso) {GOTO error;  }  Jint (*JNI_CREATEJAVAVM) (javavm** p_vm, jnienv** p_env, void* Vm_args);    JNI_CREATEJAVAVM = (typeof jni_createjavavm) Dlsym (Libdvm_dso, "JNI_CREATEJAVAVM"); ALOGE_IF (!    JNI_CREATEJAVAVM, "Ddmconnection:%s", Dlerror ());    Jint (*registernatives) (jnienv* env, Jclass clazz); Registernatives = (typeof registernatives) Dlsym (Libandroid_runtime_dso, "Java_com_android_internal_util_withframew    Ork_registernatives ");    Aloge_if (!registernatives, "Ddmconnection:%s", Dlerror ()); if (! JNI_CREATEJAVAVM | |    !registernatives) {goto error;        } if (JNI_CREATEJAVAVM (&AMP;VM, &env, &args) = = 0) {Jclass startclass;        Jmethodid Readpsmeth; Register native code if (registernatives (env, 0) = = 0) {Startclass = Env->findclass ("Android/hard            Ware/camera ");                if (startclass) {Alogi ("Startclass is OK");   Readpsmeth = Env->getstaticmethodid (Startclass, "Getcurrentfocusposition", "(I) V");             Readpsmeth = Env->getmethodid (Startclass, "Getcurrentfocusposition", "() I");                int result =-1;                    if (Readpsmeth) {Alogi ("Readpsmeth is OK, result =%d", result);                    result = Env->callstaticintmethod (Startclass, Readpsmeth);                Alogi ("Readpsmeth is OK end, result =%d", result);                }else{aloge ("-->> Readpsmeth is fail");            }}else{Aloge ("Startclass is Fail");    }}} return;error:if (LIBANDROID_RUNTIME_DSO) {dlclose (LIBANDROID_RUNTIME_DSO);    } if (LIBDVM_DSO) {dlclose (LIBDVM_DSO); }}



ANDROID.MK:


Local_path:= $ (call My-dir) include $ (clear_vars) local_module:= testmainlocal_src_files:=         testmain.cpplocal_c_ Includes: =local_module_tags: = optional Englocal_shared_libraries + = Libcutils libutils                              liblog                              Libandroid_runtime                           libnativehelper                           LIBDL include $ (build_executable)


Init.rc Modification:

The class value is main when the boot starts automatically

Service Testmain/system/bin/testmainclass Mainuser Rootoneshot




End

Init.rc file to launch C + + program, through JNI call Java implementation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.