Android如何監控本應用被卸載,android監控卸載

來源:互聯網
上載者:User

Android如何監控本應用被卸載,android監控卸載

我們知道很多應用被卸載後,都會開啟瀏覽器,請你反饋一下為何卸載他們,那他們怎麼知道呢?

我知道很多人會想到監聽卸載廣播android.intent.action.PACKAGE_REMOVED,但是你在被卸載的時候進程被kill掉,這個廣播你是來不及接受,也沒辦法處理!

另外一種去監聽剛開始卸載的log,但有可能監聽不到,或者也同樣來不及。

最理想的方式我想大家都知道,就是另外一個app監聽PACKAGE_REMOVED就可以了。

但是你只有一個app,並且不想你被卸載掉後在使用者的手機上還殘存一個app,那這個時候應該怎麼辦呢?

其實我們可以通過ndk,去寫一個natvie的進程,這個進程通過inotify監聽你的應用檔案夾是否被刪除,如果刪除就做相應處理即可。

inotify可以監聽檔案是否被刪除修改開啟等等操作,我們知道一個app會在/data/data/目錄下用包名建立一個檔案夾,我們應用為com.test.test,所以監聽/data/data/com.test/test這個檔案夾就可以了。

最後的處理比較簡單,就是上報伺服器,至於上報哪些內容就要看需求了。

對於inotify的使用可參考:http://www.ibm.com/developerworks/cn/linux/l-inotify/

代碼參閱如下:

#include <sys/socket.h>#include <stdio.h>#include <android/log.h>#include <sys/inotify.h>#include <sys/select.h>#include <errno.h>#include <stdlib.h>#include <stdint.h>#include <fcntl.h>#include <sys/ioctl.h>#include <unistd.h>#include <sys/stat.h>#include <sys/types.h>#include <stddef.h>#include <string.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#define LOG_TAG "JNI_LEARN"#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args)#define DEST_PORT 12234#define DEST_IP_ADDR "10.78.137.21"#define CS_MOBILE_UNINSTALLID 1007void handle_event(struct inotify_event *pevent){if(pevent->mask & IN_DELETE_SELF){LOGD("deleted!");int sockfd;struct sockaddr_in s_add;int sin_size;sockfd = socket(AF_INET, SOCK_DGRAM, 0);if(-1 == sockfd){LOGD("socket fail ! \r\n");exit(1);}LOGD("socket ok !\r\n");bzero(&s_add,sizeof(struct sockaddr_in));s_add.sin_family=AF_INET;s_add.sin_addr.s_addr = inet_addr(DEST_IP_ADDR);s_add.sin_port=htons(DEST_PORT);int data = CS_MOBILE_UNINSTALLID;int dest_len = sizeof(struct sockaddr_in);int send_num = sendto(sockfd, (void *)&data, 4, 0, (struct sockaddr *)&s_add, dest_len);if(send_num < 0){LOGD("sendto fail!");}else{LOGD("sendto ok!");exit(0);}}}int main(int argc, char **argv){LOGD("uninstall process watching...");int fd = inotify_init();const char* watchPath = "/data/data/com.test.test";int wd = inotify_add_watch(fd, watchPath, IN_ALL_EVENTS);const int BUF_LEN = 16384;char buffer[BUF_LEN];struct inotify_event *pevent;while(true){fd_set fds;FD_ZERO(&fds);FD_SET(fd, &fds);//堵塞事件,當有事件發生時傳回值大於0if(select(fd +1, &fds, NULL, NULL, NULL) > 0){int len, index = 0;if((len = read(fd, buffer, BUF_LEN)) < 0)break;while(index < len){pevent = (struct inotify_event *)(buffer + index);LOGD("event dispatch");//處理事件handle_event(pevent);index += (offsetof(struct inotify_event, name) + pevent->len);}}}    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.