1、recovery函数:

#define	UPDATE_TITLE			"--update_package="
#define UPDATE_COMMAND_FILE "/cache/recovery/command"
#define UPDATE_FLAG_FILE "/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 ALOGD int 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_log
if (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_CMD_RESTART2, (void*) "recovery");//这句须要root权限!
printf("[%s]reboot failed\n", __FUNCTION__);
return -7;
}

2、OTA升级函数

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_FILE
memset(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(path);
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_FILE
memset(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);
return -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_log
if (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");//相同须要root权限
printf("[%s]reboot failed\n", __FUNCTION__);
return -7;
}

3、事实上上面两个函数假设编译成exe在root下执行确实能够实现recovery和OTA升级。怎样在jni或者apk中掉用和实现了?

3.1 apk申请system权限,须要签名或者在源代码中编译!

3.2 apk是无法直接获取到root权限的,最多system权限,因此我们能够採service!

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

3.3 将上面的函数写两个应用。编译后放在/system/bin/下,这样我们就可以在jni中或apk中去开启service:

init.rc中:

servicerecovery
/system/bin/recovery

disabled

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

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

3.4这样就能够实现recovery,OTA了!

android JNI库实现reboot,recovery的更多相关文章

  1. Android JNI如何调用第三方库

    http://www.2cto.com/kf/201504/388764.html Android JNI找不到第三方库的解决方案 cannot load library 最近做一个jni项目,拿到的 ...

  2. 【转】Android下编译jni库的二种方法(含示例)

    原文网址:http://blog.sina.com.cn/s/blog_3e3fcadd01011384.html 总结如下:两种方法是:1)使用Android源码中的Make系统2)使用NDK(从N ...

  3. 【转】Android下编译jni库的二种方法(含示例) -- 不错

    原文网址:http://blog.sina.com.cn/s/blog_3e3fcadd01011384.html 总结如下:两种方法是:1)使用Android源码中的Make系统2)使用NDK(从N ...

  4. android开发源代码分析--多个activity调用多个jni库的方法

    android开发源代码分析--多个activity调用多个jni库的方法 有时候,我们在开发android项目时会遇到须要调用多个native c/jni库文件,下面是本人以前实现过的方法,假设有知 ...

  5. Android 4.4.2 动态加入JNI库方法记录 (二 app应用层)

    欢迎转载,务必注明出处:http://blog.csdn.net/wang_shuai_ww/article/details/44458553 源代码下载地址:http://download.csdn ...

  6. 获取Android APK JNI库

    /************************************************************************** * 获取Android APK JNI库 * 说 ...

  7. Android 4.4.2 动态加入JNI库方法记录 (一 JNI库层)

    欢迎转载,务必注明出处.http://blog.csdn.net/wang_shuai_ww/article/details/44456755 本篇是继<s5p4418 Android 4.4. ...

  8. 利用gdb 调试android jni c动态库

    http://blog.dornea.nu/2015/07/01/debugging-android-native-shared-libraries/ Since I haven't done thi ...

  9. [转载]—— Android JNI知识点

    Java Native Interface (JNI)标准是java平台的一部分,它允许Java代码和其他语言写的代码进行交互.JNI 是本地编程接口,它使得在 Java 虚拟机 (VM) 内部运行的 ...

随机推荐

  1. 转(havel 算法)

    http://www.cnblogs.com/wally/p/3281361.html poj 1659(havel算法) 题目链接:http://poj.org/problem?id=1659 思路 ...

  2. 表格td标签在不添加多余标签的情况下实现文本内容单行显示,多余部分省略号表示的方法

    #table { table-layout: fixed; } .content { white-space: nowrap; text-overflow: ellipsis; -o-text-ove ...

  3. iOS 实时监听app的网络连接状态

    实际iOS开发中,在网络通信中我们大部分使用第三方(只谈短链),譬如 AFNetworking.ASIHttpRequest(这个停更了,想必现在没多少人用),swift的 Alamofire 等. ...

  4. Co-prime Array&&Seating On Bus(两道水题)

     Co-prime Array Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

  5. activity之栈管理

    在android中.一个activity组件能够激活还有一个activity组件:本程序activity和其他程序的activity.     若新的被激活的activity组件属于还有一个应用程序, ...

  6. 将数组,表的某一列转换为string字符串的方法

    样例:字符串数组为array,str为字符串数组转换成的字符串 string[] array = { etr, kdgj, 3454, tyt, gff }; string str=string.Jo ...

  7. OSError: [Errno 13] Permission denied: '/etc/cron.d/1sandbox_registration'

    使用Hortonworks 的twitter tutorial: http://hortonworks.com/hadoop-tutorial/how-to-refine-and-visualize- ...

  8. Android系统休眠对程序的影响以及处理

    Android系统在用户长时间不操作时,为了节省资源,系统会选择休眠.在休眠过程中自定义的Timer.Handler.Thread.Service等都会暂停.而有时候这种机制会影响到我们程序的正常运行 ...

  9. Ubuntu修改计算机名称造成无法解析主机问题解决方法

    在通过修改/etc/hostname文件方法修改计算机名称后导致有时候出现无法解析主机的问题. 解决方法: 找到/etc/hosts文件,打开找到如下一行 127.0.1.1       旧主机名 将 ...

  10. 不规则三角网 Delaunay——TIN

    http://blog.csdn.net/u010025211/article/details/25032209 知识点一:平面中判断一个点是否在三角形内部. #include <stdio.h ...