Android JNI library implements reboot, recovery, and androidjni

Source: Internet
Author: User

Android JNI library implements reboot, recovery, and androidjni

1. recovery function:

# DefineUPDATE_TITLE "-- update_package =" # define UPDATE_COMMAND_FILE "/cache/recovery/command" # define logs "/cache/recovery/last_flag" # define LAST_INSTALL_FILE "/cache/recovery/last_install" # define LAST_LOG_FILE "/cache/recovery/last_log" # define LAST_LOCALE_FILE "/cache/recovery/last_locale" # define printf ALOGDint factory_data_reset (void) {char data [] = {"-- wipe_data \ n -- locale = en_US \ n"}; int len = 0, fd; printf ("[% s] command: % s \ n", _ FUNCTION __, data); fd = open (UPDATE_COMMAND_FILE, O_RDWR | O_CREAT | O_TRUNC, s_IRUSR | S_IWUSR); if (fd <0) {printf ("[% s] creat command file failed \ n", _ FUNCTION _); return-3 ;} len = strlen (data); if (write (fd, data, len )! = Len) {printf ("[% s] write command file failed \ n", _ FUNCTION _); close (fd); return-4 ;} close (fd); // delete last_install, last_logif (remove (LAST_INSTALL_FILE )! = 0) printf ("[% s] remove last_install failed \ n", _ FUNCTION _); if (remove (LAST_LOG_FILE )! = 0) printf ("[% s] remove last_log failed \ n", _ FUNCTION _); if (remove (LAST_LOCALE_FILE )! = 0) printf ("[% s] remove last_locale failed \ n", _ FUNCTION _); sync (); // reboot to recovery _ reboot (LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, linux_reboot_assist_restart2, (void *) "recovery"); // root permission is required! Printf ("[% s] reboot failed \ n", _ FUNCTION _); return-7 ;}

2. OTA update functions

Int install_ota_package (char const * package_file, int use_fuse) {char * path = NULL; int len = 0, size, fd; len = strlen (package_file ); if (len <= 0) {printf ("[% s] strlen (package_file) = % d \ n", _ FUNCTION __, len); return-1 ;} path = (char *) malloc (len + 24 + 3); if (path = 0) {printf ("[% s] malloc failed \ n ", _ FUNCTION _); return-2;} // UPDATE_COMMAND_FILEmemset (path, 0, len + 24 + 3); if (use_fuse) // (strncmp (package_file, "/Vtfuse", 7 )! = 0) {strcpy (path, "-- update_package =/vtfuse"); strcpy (& path [24], package_file); strcpy (& path [24 + len], "\ n");} else {strcpy (path, "-- update_package ="); strcpy (& path [17], package_file ); strcpy (& path [17 + len], "\ n");} printf ("[% s] command: % s \ n", _ FUNCTION __, path ); fd = open (UPDATE_COMMAND_FILE, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); if (fd <0) {printf ("[% s] creat command file failed \ n ", _ FUNCTION _); free (pa Th); return-3;} size = strlen (path); if (write (fd, path, size )! = Size) {printf ("[% s] write command file failed \ n", _ FUNCTION _); free (path); close (fd ); return-4;} close (fd); // UPDATE_FLAG_FILEmemset (path, 0, len + 24 + 3); if (use_fuse) // (strncmp (package_file, "/vtfuse", 7 )! = 0) {strcpy (path, "updating $ path =/vtfuse"); strcpy (& path [21], package_file); strcpy (& path [21 + len], "\ n");} else {strcpy (path, "updating $ path ="); strcpy (& path [14], package_file ); strcpy (& path [14 + len], "\ n");} printf ("[% s] last_flag: % s \ n", _ FUNCTION __, path ); fd = open (UPDATE_FLAG_FILE, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); if (fd <0) {printf ("[% s] creat last_flag file failed \ n ", _ FUNCTION _); free (path); r Eturn-5;} size = strlen (path); if (write (fd, path, size )! = Size) {printf ("[% s] write last_flag file failed \ n", _ FUNCTION _); free (path); close (fd ); return-6;} close (fd); // delete last_install, last_logif (remove (LAST_INSTALL_FILE )! = 0) printf ("[% s] remove last_install failed \ n", _ FUNCTION _); if (remove (LAST_LOG_FILE )! = 0) printf ("[% s] remove last_log failed \ n", _ FUNCTION _); sync (); free (path ); // reboot to recovery _ reboot (LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void *) "recovery "); // also requires the root permission printf ("[% s] reboot failed \ n", _ FUNCTION _); return-7 ;}


3. In fact, if the above two functions are compiled into exe and run under the root, the recovery and OTA upgrades can be implemented. How can we remove and implement the functions in jni or apk?

3.1 apk: Apply for system permission. You need to sign the permission or compile it in the source code!


3.2 apk cannot directly obtain the root permission. A maximum of system permissions are available. Therefore, we can use the service!

Reference: http://blog.chinaunix.net/uid-12348673-id-3030823.html


3.3 write the above functions into two applications and put them under/system/bin/after compilation, so that we can enable service in jni or apk:

In init. rc:

Servicerecovery/system/bin/recovery

Disabled


Apk: SystemProperties. set ("ctl. start", "recovery ");



Jni: property_set ("ctl. start", "recovery ");


In this way, you can implement recovery and OTA!





Help write an android JNI to call the android main program (AndroidActivity) of the so Library)

Don't understand what you need
Public native int Initialize (); // return success is 1, failure is 0
Public native void Uninitialize (); // destroy
Public native void SetFaceDetectParameters (int sensibility); // sensibility 3-low, 5-medium, 7-high
Public native int DetectFaces (byte [] picdata); // return the detection result
Public native String test (); // test whether the lib connection is successful. Return OK directly.
This is the call method.

Android system reity <3e> android system recovery util

This is not the flash mode, but the Recovery mode.

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.