We know that many applications are uninstalled and will open the browser, so please give us some feedback on why they were uninstalled, so how do they know?
I know a lot of people will think of listening to uninstall the broadcast Android.intent.action.PACKAGE_REMOVED, but you are uninstalled when the process was killed, this broadcast you are too late to accept, there is no way to deal with!
The other is to listen to the log that just started unloading, but it may not be able to listen, or too late.
The ideal way I think we all know, is another app monitoring package_removed can be.
But you have only one app, and do not want you to be uninstalled after the user's mobile phone still have an app, what should be done this time?
In fact, we can go through the NDK, to write a natvie process, the process through INotify monitoring your application folder is deleted, if you delete the corresponding processing.
INotify can listen to the file is deleted modify open and so on, we know an app in the/data/data/directory with the package name to create a folder, we apply as com.test.test, so listen to/data/data/com.test/ Test this folder is ready.
The final processing is relatively simple, is to report the server.
See the code below:
#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 FA Il! \ 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 = "/da Ta/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);//blockage event, when an event occurs, the return value is greater than 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");//Handling Event handle_event (pevent); index + = (offsetof (struct Inotify_evENT, name) + Pevent->len);}}} return 0;}
How Android monitors this app is uninstalled