The Android listener is uninstalled.

Source: Internet
Author: User

The Android listener is uninstalled.
Overview:

If this is not a special case, I think we will rarely be able to meet this requirement. In fact, the Java part of Android does not provide the corresponding interface. here we need to call the C code, that is, to write JNI. For more information about JNI, refer to the articles on JNI in my blog.

 

Train of Thought Analysis:

As we all know, the Android program can listen to the broadcast of the system uninstall program, but unfortunately, it cannot listen to itself being uninstalled, so what can we do after our program is uninstalled? Java didn't say how to do it. What about C?

C is acceptable. C's idea is to listen for changes to the data/[packageNmae] folder.

 

 

 
Implementation process: mainly implement code-C:

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          /* Macro definition begin * // clear 0 macro # define MEM_ZERO (pDest, destSize) memset (pDest, 0, destSize) // LOG macro definition # define LOG_INFO (tag, msg) _ android_log_write (ANDROID_LOG_INFO, tag, msg) # define LOG_DEBUG (tag, msg) _ android_log_write (ANDROID_LOG_DEBUG, tag, msg) # define LOG_WARN (tag, msg) _ android_log_write (ANDROID_LOG_WARN, tag, msg) # define LOG_ERROR (tag, msg) _ android_log_write (ANDROID_LOG_ERROR, tag, msg)/* global variable begi N */static char c_TAG [] = onEvent; static jboolean B _IS_COPY = JNI_TRUE; jstring encode (JNIEnv * env, jobject thiz, jstring path, jstring url, jint version) {jstring tag = (* env)-> NewStringUTF (env, c_TAG); // initialize logLOG_DEBUG (* env)-> GetStringUTFChars (env, tag, & B _IS_COPY ), (* env)-> GetStringUTFChars (env, (* env)-> NewStringUTF (env, init OK), & B _IS_COPY )); // Fork sub-process to execute the polling task pid_t pid = fork (); if (pid <0) {// error logLOG_ERROR (* env)-> GetStringUTFChars (env, tag, & B _IS_COPY), (* env)-> GetStringUTFChars (env, (* env)-> NewStringUTF (env, fork failed !!!), & B _IS_COPY);} else if (pid = 0) {// subprocess registration directory listener int fileDescriptor = inotify_init (); if (fileDescriptor <0) {LOG_DEBUG (* env)-> GetStringUTFChars (env, tag, & B _IS_COPY), (* env)-> GetStringUTFChars (env, (* env)-> NewStringUTF (env, inotify_init failed !!!), & B _IS_COPY); exit (1);} int watchDescriptor; watchDescriptor = inotify_add_watch (fileDescriptor, (* env)-> GetStringUTFChars (env, path, NULL), IN_DELETE ); if (watchDescriptor <0) {LOG_DEBUG (* env)-> GetStringUTFChars (env, tag, & B _IS_COPY), (* env)-> GetStringUTFChars (env, (* env) -> NewStringUTF (env, inotify_add_watch failed !!!), & B _IS_COPY); exit (1) ;}// allocate the cache to read the event. The cache size = the size of a struct inotify_event, in this case, an eventvoid * p_buf = malloc (sizeof (struct inotify_event); if (p_buf = NULL) {LOG_DEBUG (* env)-> GetStringUTFChars (env, tag, & B _IS_COPY), (* env)-> GetStringUTFChars (env, (* env)-> NewStringUTF (env, malloc failed !!!), & B _IS_COPY); exit (1) ;}// start listening to LOG_DEBUG (* env)-> GetStringUTFChars (env, tag, & B _IS_COPY), (* env) -> GetStringUTFChars (env, (* env)-> NewStringUTF (env, start observer), & B _IS_COPY); // read will block the process, size_t readBytes = read (fileDescriptor, p_buf, sizeof (struct inotify_event); // when you go to this page, you will receive the event of directory deletion, and cancel the listener free (p_buf); inotify_rm_watch (fileDescriptor, IN_DELETE ); // The directory does not contain logLOG_DEBUG (* env)-> GetStringUTFChars (env, tag, & B _IS_COPY), (* env)-> GetStringUTFChars (env, (* env)-> NewStringUTF (env, uninstalled), & B _IS_COPY); if (version> = 17) {// 4.2 or more systems must be added with -- user 0 execlp (am, am, start, -- user, 0,-a, android. intent. action. VIEW,-d, (* env)-> GetStringUTFChars (env, url, NULL), (char *) NULL);} else {execlp (am, am, start, -a, android. intent. action. VIEW,-d, (* env)-> GetStringUTFChars (env, url, NULL), (char *) NULL );} // Extension: You can execute other shell commands, am (activity manager), open a program, service, broadcast intent, and so on} else {// The parent process exits directly, enable the child process to be adopted by the init process to prevent the child process from freezing} return (* env)-> NewStringUTF (env, Hello from JNI !);}
        
       
      
     
    
   
  
 

Call process: UninstallObserver. java

 

 

public class UninstallObserver {static{System.loadLibrary(observer);}public static native String startWork(String path, String url, int version);}

Call process: MainActivity. java

 

 

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                listening();    }        private void listening() {        UninstallObserver.startWork(/data/data/ + getPackageName(), https://www.baidu.com, android.os.Build.VERSION.SDK_INT);    }}

 

 

Effect diagram:

 

 

 

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.