Android下开机自启动C程序【转】

来自:http://blog.csdn.net/hanbo622/article/details/49909415

本文简单介绍下在Android开机自启动C程序方法,在C程序由于某种原因退出时,并能重新启动,在不使用rild的功能的条件下,如果使用rild功能,则更改一下相应的服务即可,或者自己添加一个简单的服务。

1.把Android源码hardware/ril/rild/rild.c 替换成如下:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #define LOG_TAG "RILD"  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <unistd.h>  
  5. #include <pthread.h>  
  6. #include <utils/Log.h>  
  7. #include <sys/types.h>  
  8. #include <sys/stat.h>  
  9. #include <fcntl.h>  
  10. #include <cutils/properties.h> //property_set()  
  11.   
  12. #define PPPD_SERVICE_NAME "pppd_gprs"  
  13. #define FIFO_NAME "/system/bin/Sh_fifo"   
  14. #define INDEX_INIT 25  //多少秒数接收不到C程序的响应后重启C程序  
  15. #define COUNT_MAX  5   //多少次重启C程序不成功后重启系统  
  16. static int Index=INDEX_INIT;  
  17. static int count=0;  
  18. void *read_thread(void *arg)  
  19. {  
  20.     int fd;  
  21.     fd=open(FIFO_NAME,O_RDONLY);//打开命名管道  
  22.     if(fd<0)  
  23.     {  
  24.         ALOGW("open %s error!",FIFO_NAME);  
  25.     }  
  26.     while(1)  
  27.     {  
  28.         int recv=0;  
  29.         read(fd,&recv,sizeof(recv));  
  30.         if(recv!=0)  
  31.         {  
  32.             Index=recv;  
  33.             count=0;  
  34.         }  
  35.     }  
  36.     return NULL;  
  37. }  
  38. void *check_thread(void *arg)  
  39. {  
  40.     while(1)  
  41.     {  
  42.         Index--;  
  43.         if(Index<=0)  
  44.         {  
  45.             Index=INDEX_INIT;  
  46.             count++;  
  47.             property_set("ctl.stop", PPPD_SERVICE_NAME);//异常停止pppd_gprs  
  48.             int err = property_set("ctl.start", PPPD_SERVICE_NAME);//设置pppd_gprs属性为启动  
  49.             if (err < 0) {  
  50.                 ALOGW("Can not restart server!");  
  51.             }  
  52.             if(count>COUNT_MAX)  
  53.             {  
  54.                 system("reboot");  
  55.                 count=0;  
  56.             }  
  57.             ALOGD("restart pppd_gprs...\n");  
  58.         }  
  59. //      ALOGD("---count:%d Index:%d----\n",count,Index);  
  60.         sleep(1);  
  61.     }  
  62.     return NULL;  
  63. }  
  64. int main(int argc, char *argv[])  
  65. {  
  66.     pthread_t read_tid;  
  67.     pthread_t check_tid;  
  68.     int ret=mkfifo(FIFO_NAME,S_IRUSR|S_IWUSR);//创建命名管道  
  69.     if(ret!=0)  
  70.     {  
  71.         ALOGW("mkfifo %s error!",FIFO_NAME);  
  72.     }  
  73.       
  74.     sleep(15);  
  75.     int err = property_set("ctl.start", PPPD_SERVICE_NAME);//设置pppd_gprs属性为启动  
  76.     if (err < 0) {  
  77.         ALOGW("Can not start server!");  
  78.     }  
  79.     else  
  80.     {  
  81.         ALOGD("server started...");  
  82.     }  
  83.       
  84.     pthread_create(&read_tid,NULL,read_thread,NULL);  //创建读取数据线程  
  85.     pthread_create(&check_tid,NULL,check_thread,NULL);  //创建检测线程  
  86.     while(1){  
  87.         sleep(0x00ffffff);  
  88.     }  
  89.     return 0;  
  90. }  

 

 

2.把Android源码hardware/ril/rild/Android.mk 替换成如下:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. # Copyright 2006 The Android Open Source Project  
  2.   
  3. LOCAL_PATH:= $(call my-dir)  
  4. include $(CLEAR_VARS)  
  5.   
  6. LOCAL_SRC_FILES:= rild.c  
  7.   
  8.   
  9. LOCAL_SHARED_LIBRARIES := \  
  10.  libcutils \  
  11.  libdl  
  12.   
  13. LOCAL_LDLIBS += -lpthread  
  14. LOCAL_MODULE:= rild  
  15. LOCAL_MODULE_TAGS := optional  
  16.   
  17. include $(BUILD_EXECUTABLE)  


 3.在out/target/product/ut4412/root/init.rc中添加如下代码

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. on boot  (在这条语句下面)  
  2.  #hslong by add 2015.11.18  
  3.  chmod 777 /system/etc/hsl_sh  
  4.  chmod 777 /system/bin/server  
  5. ...  
  6. service ril-daemon /system/bin/rild  
  7.  class main  
  8. #    socket rild stream 660 root radio  
  9. #    socket rild-debug stream 660 radio system  
  10.  user root  
  11.  group radio cache inet misc audio sdcard_rw qcom_oncrpc qcom_diag log (在这条语句下面)  
  12.   
  13. #hslong by add 2015.11.18  
  14. service pppd_gprs /system/bin/c_test  
  15.  class main  
  16.  user root  
  17.  group radio cache inet misc  
  18.  disabled  
  19.  oneshot  

 

4.把编译好的程序放到/system/bin 下面
 
  测试代码如下:
  Android.mk

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. # Copyright 2006 The Android Open Source Project  
  2.   
  3.   LOCAL_PATH:= $(call my-dir)  
  4.   include $(CLEAR_VARS)  
  5.   LOCAL_LDLIBS+=-lpthread  
  6.   LOCAL_LDFLAGS := -ldl  
  7.   LOCAL_SRC_FILES :=c_test.c  
  8.   
  9.   
  10.   LOCAL_SHARED_LIBRARIES := \  
  11.    libcutils \  
  12.    libdl  
  13.   
  14.   LOCAL_MODULE:= c_test     
  15.   LOCAL_MODULE_TAGS := optional  
  16.   
  17.   include $(BUILD_EXECUTABLE)  


c_test.c

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4. #include <fcntl.h>  
  5. #include <sys/types.h>  
  6. #include <sys/stat.h>  
  7. #include <pthread.h>  
  8.   
  9. /************************************************************ 
  10.  *函数名称: int main(int argc, char *argv[]) 
  11.  *函数功能: 主函数 
  12.  *函数参数: 无 
  13.  *函数返回: 无 
  14.  ***********************************************************/  
  15. int main(int argc, char *argv[])  
  16. {  
  17.     int fifo_fd;      //声明与自启动代码通信命名管道描述符  
  18.     pthread_t tid;    //服务线程描述符  
  19.   
  20.     if((fifo_fd=open(FIFO_NAME,O_WRONLY))<0)  
  21.     {  
  22.         perror("open fifo error!\n");  
  23.         exit(-1);  
  24.     }  
  25.       
  26.     pthread_create(&tid,NULL,thread,NULL);      //创建服务线程  
  27.     pthread_detach(tid);                        //服务线程分离  
  28.   
  29.     while(1){  
  30.         int data=20;  
  31.         write(fifo_fd,&data,sizeof(data));  
  32.         sleep(10);  
  33.     }  
  34.     return 0;  
  35. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值