How does Android monitor whether the app is uninstalled and android monitor and uninstall it?

Source: Internet
Author: User
Tags inotify

How does Android monitor whether the app is uninstalled and android monitor and uninstall it?

We know that many applications will open a browser after they are uninstalled. Please tell us why they are uninstalled. How do they know?

I know many people will think of listening to uninstall broadcast android. intent. action. PACKAGE_REMOVED, but the process is killed when you are unmounted. This broadcast is too late to accept and cannot be handled!

Another method is to listen for logs that are unmounted at the beginning, but may not be listened to, or it may be too late.

The most ideal way is to listen to PACKAGE_REMOVED by another app.

But you only have one app, and you don't want to have an app remaining on your mobile phone after it is uninstalled. What should you do at this time?

In fact, we can use ndk to write a natvie process. This process uses inotify to monitor whether your application folder has been deleted. If it is deleted, perform corresponding processing.

Inotify can be used to monitor whether a file is deleted, modified, opened, and so on. We know that an app will create a folder with a package name under the/data/directory, and our app is com. test. test, so listening for/data/com. you can use the test/test folder.

The final process is relatively simple, that is, reporting to the server. As for what to report, it depends on the requirement.

For the use of inotify, see: http://www.ibm.com/developerworks/cn/linux/l-inotify/

See the following code:

# 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_LEAR N "# define LOGD (fmt, args ...) _ android_log_print (ANDROID_LOG_DEBUG, LOG_TAG, fmt, ## args) # define DEST_PORT 12234 # define logs "events" # define logs 1007 void 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; region = inet_addr (region); 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/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); // block an event. when an event occurs, the returned 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"); // handle event handle_event (pevent); index + = (offsetof (struct inotify_event, name) + pevent-> len) ;}} return 0 ;}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.