<span style= "FONT-SIZE:24PX;" > Calling a local function is implemented by C + + </span></span>
Package Com.lapel.catchuninstallself;public class Uninstallobserver {static {system.loadlibrary ("Observer");}/** * * @param path Local Installation apk path * @param URL popup page after uninstall * @param version of this machine * @return */public static Native string Startwork (string path, string url, int version);}
</pre><pre name= "code" class= "HTML" ><span style= "FONT-SIZE:24PX;" >c/c++ Implementation </span></span>
<pre name= "code" class= "HTML" >/* * Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. * */#include <string.h> #include <jni.h> #include <jni.h> #include <stdio.h> #include < stdlib.h> #include <string.h> #include <android/log.h> #include <unistd.h> #include <sys/ INOTIFY.H>/* macro definition begin *///0 Macro # define Mem_zero (PDest, Destsize) memset (pDest, 0, destsize)//log macros define # 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)/* Inside global variable begin */static char c_tag[] = "onEvent"; static Jboolean b_is_copy = jni_true;//Original Package com. Zgy.catchuninstallself----------java_com_zgy_catchuninstallself_uninstallobserver_startwork//Present Package Com.lapel.catchuninstallselfjstring java_com_lapel_catchuninstallself_uninstallobserver_startwork (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) {//Sub-process registered Directory listener int filedescriptor = Inotify_init (); if (FileDescriptor < 0) {Log_debug ((*env)->getst Ringutfchars (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), or//go here to indicate that a directory was deleted, Logoff 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 user rights management stricter, 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!");}
android.mk Folder
</pre><pre name= "code" class= "HTML" >local_path: = $ (call My-dir) include $ (clear_vars) local_module:= Observerlocal_src_files:=observer.clocal_c_includes:= $ (local_path)/includelocal_ldlibs + =-L$ (SYSROOT)/usr/lib- Lloginclude $ (build_shared_library)
</pre><p><span style= "FONT-SIZE:24PX;" > After running, you can generate. So files </span></p><p><span style= "font-size:18px" >system.loadlibray ("</span ><span style= "Background-color:rgb (240, 240, 240); font-size:18px; " >observer</span><span style= "FONT-SIZE:18PX;" > ")://To complete the load, and then call the local method to </span></p><pre>
Pop-up page after app Uninstall