Android不被kill的Service与卸载之后跳转出反馈页面

转载:http://blog.csdn.net/jimmylopez/article/details/41015337#comments

最近看到很多android应用卸载之后,都会弹出一个网页来填写反馈。

参考了http://www.cnblogs.com/zealotrouge/archive/2013/09/24/3182617.html上的方法,我也做了一个。现把代码展示出来。

每次接收到进程中断的消息时,父进程会通过handler通知到子进程,在子进程1中fork一个子进程2,子进程2会sleep1秒,1秒过后判断data/data/下的数据是否存在,从而区分是被其他安全软件kill掉了还是被卸载了。

由于某些手机会直接kill掉子进程1,所以才在子进程1中再fork出子进程2。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <pre name="code" class="java">#include "apkObserver.h"  
  2. #ifdef __cplusplus  
  3. extern "C"  
  4. {  
  5. #endif  
  6.   
  7. static jboolean isCopy = JNI_TRUE;  
  8. static char* rtn;  
  9. static char* url;  
  10. static char* action;  
  11. static jboolean isAvailable;  
  12. static jstring s_Searial;  
  13. static int ok = -1;  
  14. static const char OBSERVER_DIR[] = "/data/data/com.xxx.xxx";  
  15.   
  16.   
  17. void handler(int signo) {  
  18.     if (-1 != ok) {  
  19.         pid_t pid;  
  20.         pid = fork();  
  21.         if (0 == pid) {  
  22.             LOG_INFO("Jimmy""FORK");  
  23.             sleep(1);  
  24.             int lockFileDescriptor = open(OBSERVER_DIR, O_RDONLY);  
  25.             if (lockFileDescriptor == -1) {  
  26.                 LOG_INFO("Jimmy""EXIT");  
  27.                 //已被卸载,跳出反馈页面  
  28.                 if (isAvailable) {  
  29.                     if (s_Searial == NULL) {  
  30.                         execlp("am""am""start""-n", action, "-a",  
  31.                             "android.intent.action.VIEW""-d", url,  
  32.                             (char *) NULL);  
  33.                     } else {  
  34.                         execlp("am""am""start""--user", rtn, "-a",  
  35.                             "android.intent.action.VIEW""-n", action, "-d",  
  36.                             url, (char *) NULL);  
  37.                     }  
  38.                 } else {  
  39.                     if (s_Searial == NULL) {  
  40.                         execlp("am""am""start""-a",  
  41.                             "android.intent.action.VIEW""-d", url,  
  42.                             (char *) NULL);  
  43.                     } else {  
  44.                         execlp("am""am""start""--user", rtn, "-a",  
  45.                             "android.intent.action.VIEW""-d", url,  
  46.                             (char *) NULL);  
  47.                     }  
  48.                 }  
  49.             }else{  
  50.                 LOG_INFO("Jimmy""RESTART");  
  51.                 //发送intent把service重新启动起来  
  52.                 if (s_Searial == NULL) {  
  53.                     execlp("am""am""startservice""-n",  
  54.                             "com.xxx.xxx/com.xxx.service.xxx",  
  55.                             (char *) NULL);  
  56.                 } else {  
  57.                     execlp("am""am""startservice""--user", rtn, "-n",  
  58.                             "com.xxx.xxx/com.xxx.service.xxx",  
  59.                             (char *) NULL);  
  60.                 }  
  61.   
  62.             }  
  63.         }else if(pid < 0){  
  64.             LOG_INFO("Jimmy""FORK ERROR");  
  65.             exit(-1);  
  66.         }else{  
  67.             exit(0);  
  68.         }  
  69.     }  
  70. }  
  71.   
  72. JNIEXPORT  
  73. int JNICALL  
  74. Java_com_xxx_xxx_MyApkObserver_init(JNIEnv *env, jobject obj,  
  75.         jstring userSerial, jstring intentURL, jboolean isAvail,  
  76.         jstring intentAction) {  
  77.     s_Searial = userSerial;  
  78.     isAvailable = isAvail;  
  79.     if (userSerial != NULL) {  
  80.         rtn = (*env)->GetStringUTFChars(env, userSerial, &isCopy);  
  81.     }  
  82.     if (intentURL != NULL) {  
  83.         url = (*env)->GetStringUTFChars(env, intentURL, &isCopy);  
  84.     }  
  85.     if (isAvail && intentAction != NULL) {  
  86.         action = (*env)->GetStringUTFChars(env, intentAction, &isCopy);  
  87.     }  
  88.     pid_t pp = getpid();  
  89.     pid_t pid;  
  90.     pid = fork();  
  91.     if (0 == pid) {  
  92.         signal(SIGCHLD, handler);  
  93.         ptrace(PTRACE_ATTACH, pp, NULL, NULL);  
  94.         wait(NULL);  
  95.         ptrace(PTRACE_CONT, pp, NULL, NULL);  
  96.         ok = 1;  
  97.         pause();  
  98.     }else if(pid < 0){  
  99.         LOG_INFO("Jimmy""MAIN FORK ERROR");  
  100.         exit(-1);  
  101.     }  
  102.   
  103. }  
  104.   
  105. #ifdef __cplusplus  
  106. }  
  107. #endif  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值