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. 不可或缺的企业OA面临问题,以及解决建议 软件定制开发 森普演示平台

    ---恢复内容开始--- 随着信息时代的来临,企业管理也相应的信息化,各种管理软件相继而出,各行各业的信息化有过成功,也有过失败(注:是以该项目是否达到用户的预期目标而言).据统计在信息化失败的案例中 ...

  2. wpf Visibility 动画

    XAML实现: [xhtml] view plaincopy <Border Height="100" Width="80" CornerRadius=& ...

  3. H5单页面架构:requirejs + angular + angular-route

    说到项目架构,往往要考虑很多方面: 方便.例如使用jquery,必然比没有使用jquery方便很多,所以大部分网站都接入类似的库: 性能优化.包括加载速度.渲染效率: 代码管理.大型项目需要考虑代码的 ...

  4. Windows下安装Memcache

    安装步骤的时候只需要做两步: 第一步:安装memcache.exe 服务. 第二步:安装php_memcache.dll扩展,让php支持memcache. 1.安装 memcache.exe 服务 ...

  5. transform的2D部分,嗯…就这个标题了。

    上一次写了transition的内容,这次就写拼写很类似的另外一个属性transform好了……我英语差这件事就不要吐槽了,下面是正文,真的: transition是过渡,transform是变换. ...

  6. 安卓开发之非常好用的AndroidOne框架DownloadManager

    AndroidOne框架是采用MVC模式,集成了Android主流开源技术及组件,是一款极速且简单高效开发框架,整个项目包含两个部分AndroidOne,oneCore AndroidOne为演示项目 ...

  7. 使用react-native做一个简单的应用-01项目介绍

    学习react-native也有一个月的时间了.当学习了关于react-native的基础知识之后,打算自己去仿一个应用去练手.于是花了10天左右的时间,这个小应用的基本功能也实现的差不多了. 在展示 ...

  8. 宏定义 define

    #define kOut -1 用一个字符串代替一个数据 用kOut表示-1(一般开头有一个小写的k) 作用: 1.为了让一些数据有意义 #define kUseId asdjlfdjafa #def ...

  9. servlet 之 返回json数据并显示

    //实体类import java.util.ArrayList; public class ObjectType { private String type; private ArrayList< ...

  10. Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node Total Accepted: 72323 Total Submissions: 199207 Difficul ...