1. Implementing functions via JNI
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)/* Within the global variable begin */static char c_tag[] = "On Event "; static Jboolean b_is_copy = jni_true;jstring java_com_example_uninstallself_observer_register (JNIEnv* env, Jobject thiz, jstring path, jstring URL, jint version) {jstring tag = (*env)->newstringutf (env, c_tag);//Initialize Loglog_debu G ((*env)->getstringutfchars (env, tag, &b_is_copy), (*env)->getstringutfchars (env, (*ENV)- Newstringutf (env, "init OK"), &b_is_copy));//fork child process to perform 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) {//Child process Register 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);} Allocates the cache so that the event is read, the cache size = the size of a struct inotify_event, such that a eventvoid *p_buf = malloc (sizeof (struct inotify_event)) is processed at a time; 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 Monitoring 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));//Go here to indicate the event that the directory was deleted, Logout listener free (P_BUF), Inotify_rm_watch (FileDescriptor, in_delete);// Directory does not exist Loglog_debug ((*env)->getstringutfchars (env, tag, &b_is_copy), (*env)->getstringutfchars (env, (*ENV) ->newstringutf (env, "uninstalled"), &b_is_copy)); if (version >= 17) {//4.2 above system due to more stringent user rights management, need to add--user 0EXECLP ("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);} Extensions: Can execute other shell commands, am (i.e. Activity Manager), can open a program, service, broadcast intent, etc.} else {//parent process exits directly, so that the child process is adopted by the INIT process to avoid child process zombie} Return (*ENV)->newstringutf (env, "Hello from JNI!");}2. Define Uninstallobserver
public class Uninstallobserver {static{system.loadlibrary ("Observer");} /*** * * @param path needs to be monitored by getapplicationcontext (). Getfilesdir (). GetPath () * @param URL Offload http * @param versi On Android.os.Build.VERSION.SDK_INT * @return */public static native string register (string path, string url, INT VERSION) ;}
3. Simple to use
@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Toast.maketext (Getapplicationcontext (), Getapplicationcontext (). Getfilesdir (). GetPath () + "," + Build.VERSION.SDK_ INT, 1). Show (); Long a = System.currenttimemillis (); String str = uninstallobserver.register (Getapplicationcontext (). Getfilesdir (). GetPath (), "http://www.baidu.com", Android.os.Build.VERSION.SDK_INT); Long B = System.currenttimemillis (); Toast.maketext (Getapplicationcontext (), str+ "," + (B-A), 1). Show ();
Android_ monitoring itself application is uninstalled