原文地址:http://www.cnblogs.com/zealotrouge/archive/2013/09/24/3182617.html
Android应用如何监听自己是否被卸载及卸载反馈功能的实现(第三版)
两个月前发了两篇有关监听自己是否被卸载和卸载反馈功能实现的博客,第二版的地址如下:http://www.cnblogs.com/zealotrouge/p/3159772.html,感谢@whiletrue_童鞋发现了我的代码在4.2.2系统上无法实现卸载反馈,经过调试,有了问题的解决方案,但是由于发完博客后即处于闭关开发阶段,没时间打理博客,所以解决方案迟迟没有与大家见面,最近空闲下来,将解决思路及方案发出来给大家看看还有没有问题。
调试发现,监听依然没有问题,毕竟是Linux Kernel中的接口,Framework层再怎么改也改不到那儿去,那么问题出在哪呢?阻塞结束后,通过调用exec函数发出am命令调起浏览器访问网页,在API16(Android 4.1.x)的设备上尚可正常访问网页,而API17(Android 4.2.x)的设备上连浏览器也不能调起。
通过分析log,发现了一条线索,如下面的log的所示:
W/ActivityManager( 387): Permission Denial: startActivity asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
log中直接给出提示,需要加一个权限INTERACT_ACROSS_USERS_FULL,这个权限时API17新引入的,目的在于允许不同用户的应用之间可以产生交互。可是加上去之后发现,还不是无法调起浏览器,而且log依然提示需要权限INTERACT_ACROSS_USERS_FULL,很是奇怪,于是继续分析。
首先说明一下Linux中的pid和uid,以及android扩展的userSerialNumber。pid是Process的标识,用于系统对进程的控制,从API层面看就是用于Process.killProcess()和Process.sendSignal();uid在Linux系统中是用来标识用户的,而在android将uid视为app的标识id,用于"sandbox"安全模型,即用于app权限控制;而对于API17引入的多用户支持(目前只支持平板),uid已经被占用,只好新引入userSerialNumber来标识用户。
回到刚才的问题,log中告知startActivity时运行用户标识为-2,而调用却是由用户标识0发起,导致拒绝执行。用这句话搜索,发现在Google开发者网站中有相关的issue,链接如下:https://code.google.com/p/android/issues/detail?id=39801(打不开可以把https改为http)。结合官方的回答,问题原因如下:由于被卸载,C端进程监听到目录被删除,立即执行am命令,此时将会默认以USER_CURRENT的身份执行,由于API17中ActivityManagerService.handleIncomingUser()会校验userSerialNumber,发现用户标识不匹配,导致权限校验失败——这也说明了权限的影响范围仅限于Java端的进程,对于fork()出来的C端进程来说,并不继承父进程在Android中声明的权限。
解决方案:增加处理分支,若API>=17,将userSerialNumber传递给C端进程,然后在am命令中带上参数--user userSerialNumber即可。
Java端代码如下:
1 package main.activity; 2 3 import java.lang.reflect.InvocationTargetException; 4 import java.lang.reflect.Method; 5 6 import pym.test.uninstalledobserver.R; 7 import android.app.Activity; 8 import android.os.Build; 9 import android.os.Bundle; 10 import android.util.Log; 11 12 /** 13 * @author pengyiming 14 * @note 监听此应用是否被卸载,若被卸载则弹出卸载反馈 15 * @note 由于API17加入多用户支持,原有命令在4.2及更高版本上执行时缺少userSerial参数,特此修改 16 * 17 */ 18 19 public class UninstalledObserverActivity extends Activity 20 { 21 /* 数据段begin */ 22 private static final String TAG = "UninstalledObserverActivity"; 23 24 // 监听进程pid 25 private int mObserverProcessPid = -1; 26 /* 数据段end */ 27 28 /* static */ 29 // 初始化监听进程 30 private native int init(String userSerial); 31 static 32 { 33 Log.d(TAG, "load lib --> uninstalled_observer"); 34 System.loadLibrary("uninstalled_observer"); 35 } 36 /* static */ 37 38 /* 函数段begin */ 39 @Override 40 public void onCreate(Bundle savedInstanceState) 41 { 42 super.onCreate(savedInstanceState); 43 44 setContentView(R.layout.uninstalled_observer_layout); 45 46 // API level小于17,不需要获取userSerialNumber 47 if (Build.VERSION.SDK_INT < 17) 48 { 49 mObserverProcessPid = init(null); 50 } 51 // 否则,需要获取userSerialNumber 52 else 53 { 54 mObserverProcessPid = init(getUserSerial()); 55 } 56 } 57 58 @Override 59 protected void onDestroy() 60 { 61 super.onDestroy(); 62 63 // 示例代码,用于结束监听进程 64 // if (mObserverProcessPid > 0) 65 // { 66 // android.os.Process.killProcess(mObserverProcessPid); 67 // } 68 } 69 70 // 由于targetSdkVersion低于17,只能通过反射获取 71 private String getUserSerial() 72 { 73 Object userManager = getSystemService("user"); 74 if (userManager == null) 75 { 76 Log.e(TAG, "userManager not exsit !!!"); 77 return null; 78 } 79 80 try 81 { 82 Method myUserHandleMethod = android.os.Process.class.getMethod("myUserHandle", (Class<?>[]) null); 83 Object myUserHandle = myUserHandleMethod.invoke(android.os.Process.class, (Object[]) null); 84 85 Method getSerialNumberForUser = userManager.getClass().getMethod("getSerialNumberForUser", myUserHandle.getClass()); 86 long userSerial = (Long) getSerialNumberForUser.invoke(userManager, myUserHandle); 87 return String.valueOf(userSerial); 88 } 89 catch (NoSuchMethodException e) 90 { 91 Log.e(TAG, "", e); 92 } 93 catch (IllegalArgumentException e) 94 { 95 Log.e(TAG, "", e); 96 } 97 catch (IllegalAccessException e) 98 { 99 Log.e(TAG, "", e); 100 } 101 catch (InvocationTargetException e) 102 { 103 Log.e(TAG, "", e); 104 } 105 106 return null; 107 } 108 /* 函数段end */ 109 }
核心——native方法头文件:
1 /* 头文件begin */ 2 #include <jni.h> 3 #include <stdlib.h> 4 #include <stdio.h> 5 #include <string.h> 6 #include <unistd.h> 7 #include <fcntl.h> 8 #include <sys/inotify.h> 9 #include <sys/stat.h> 10 11 #include <android/log.h> 12 /* 头文件end */ 13 14 /* 宏定义begin */ 15 //清0宏 16 #define MEM_ZERO(pDest, destSize) memset(pDest, 0, destSize) 17 18 //LOG宏定义 19 #define LOG_INFO(tag, msg) __android_log_write(ANDROID_LOG_INFO, tag, msg) 20 #define LOG_DEBUG(tag, msg) __android_log_write(ANDROID_LOG_DEBUG, tag, msg) 21 #define LOG_WARN(tag, msg) __android_log_write(ANDROID_LOG_WARN, tag, msg) 22 #define LOG_ERROR(tag, msg) __android_log_write(ANDROID_LOG_ERROR, tag, msg) 23 /* 宏定义end */ 24 25 #ifndef _Included_main_activity_UninstalledObserverActivity 26 #define _Included_main_activity_UninstalledObserverActivity 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 #undef main_activity_UninstalledObserverActivity_MODE_PRIVATE 32 #define main_activity_UninstalledObserverActivity_MODE_PRIVATE 0L 33 #undef main_activity_UninstalledObserverActivity_MODE_WORLD_READABLE 34 #define main_activity_UninstalledObserverActivity_MODE_WORLD_READABLE 1L 35 #undef main_activity_UninstalledObserverActivity_MODE_WORLD_WRITEABLE 36 #define main_activity_UninstalledObserverActivity_MODE_WORLD_WRITEABLE 2L 37 #undef main_activity_UninstalledObserverActivity_MODE_APPEND 38 #define main_activity_UninstalledObserverActivity_MODE_APPEND 32768L 39 #undef main_activity_UninstalledObserverActivity_MODE_MULTI_PROCESS 40 #define main_activity_UninstalledObserverActivity_MODE_MULTI_PROCESS 4L 41 #undef main_activity_UninstalledObserverActivity_BIND_AUTO_CREATE 42 #define main_activity_UninstalledObserverActivity_BIND_AUTO_CREATE 1L 43 #undef main_activity_UninstalledObserverActivity_BIND_DEBUG_UNBIND 44 #define main_activity_UninstalledObserverActivity_BIND_DEBUG_UNBIND 2L 45 #undef main_activity_UninstalledObserverActivity_BIND_NOT_FOREGROUND 46 #define main_activity_UninstalledObserverActivity_BIND_NOT_FOREGROUND 4L 47 #undef main_activity_UninstalledObserverActivity_BIND_ABOVE_CLIENT 48 #define main_activity_UninstalledObserverActivity_BIND_ABOVE_CLIENT 8L 49 #undef main_activity_UninstalledObserverActivity_BIND_ALLOW_OOM_MANAGEMENT 50 #define main_activity_UninstalledObserverActivity_BIND_ALLOW_OOM_MANAGEMENT 16L 51 #undef main_activity_UninstalledObserverActivity_BIND_WAIVE_PRIORITY 52 #define main_activity_UninstalledObserverActivity_BIND_WAIVE_PRIORITY 32L 53 #undef main_activity_UninstalledObserverActivity_BIND_IMPORTANT 54 #define main_activity_UninstalledObserverActivity_BIND_IMPORTANT 64L 55 #undef main_activity_UninstalledObserverActivity_BIND_ADJUST_WITH_ACTIVITY 56 #define main_activity_UninstalledObserverActivity_BIND_ADJUST_WITH_ACTIVITY 128L 57 #undef main_activity_UninstalledObserverActivity_CONTEXT_INCLUDE_CODE 58 #define main_activity_UninstalledObserverActivity_CONTEXT_INCLUDE_CODE 1L 59 #undef main_activity_UninstalledObserverActivity_CONTEXT_IGNORE_SECURITY 60 #define main_activity_UninstalledObserverActivity_CONTEXT_IGNORE_SECURITY 2L 61 #undef main_activity_UninstalledObserverActivity_CONTEXT_RESTRICTED 62 #define main_activity_UninstalledObserverActivity_CONTEXT_RESTRICTED 4L 63 #undef main_activity_UninstalledObserverActivity_RESULT_CANCELED 64 #define main_activity_UninstalledObserverActivity_RESULT_CANCELED 0L 65 #undef main_activity_UninstalledObserverActivity_RESULT_OK 66 #define main_activity_UninstalledObserverActivity_RESULT_OK -1L 67 #undef main_activity_UninstalledObserverActivity_RESULT_FIRST_USER 68 #define main_activity_UninstalledObserverActivity_RESULT_FIRST_USER 1L 69 #undef main_activity_UninstalledObserverActivity_DEFAULT_KEYS_DISABLE 70 #define main_activity_UninstalledObserverActivity_DEFAULT_KEYS_DISABLE 0L 71 #undef main_activity_UninstalledObserverActivity_DEFAULT_KEYS_DIALER 72 #define main_activity_UninstalledObserverActivity_DEFAULT_KEYS_DIALER 1L 73 #undef main_activity_UninstalledObserverActivity_DEFAULT_KEYS_SHORTCUT 74 #define main_activity_UninstalledObserverActivity_DEFAULT_KEYS_SHORTCUT 2L 75 #undef main_activity_UninstalledObserverActivity_DEFAULT_KEYS_SEARCH_LOCAL 76 #define main_activity_UninstalledObserverActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L 77 #undef main_activity_UninstalledObserverActivity_DEFAULT_KEYS_SEARCH_GLOBAL 78 #define main_activity_UninstalledObserverActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L 79 80 /* 81 * Class: main_activity_UninstalledObserverActivity 82 * Method: init 83 * Signature: (Ljava/lang/String;)V 84 */ 85 JNIEXPORT int JNICALL Java_main_activity_UninstalledObserverActivity_init(JNIEnv *, jobject, jstring); 86 87 #ifdef __cplusplus 88 } 89 #endif 90 #endif
核心——native方法实现:
1 /* 头文件begin */ 2 #include "main_activity_UninstalledObserverActivity.h" 3 /* 头文件end */ 4 5 #ifdef __cplusplus 6 extern "C" 7 { 8 #endif 9 10 /* 内全局变量begin */ 11 static char TAG[] = "UninstalledObserverActivity.init"; 12 static jboolean isCopy = JNI_TRUE; 13 /* 内全局变量 */ 14 15 /* 16 * Class: main_activity_UninstalledObserverActivity 17 * Method: init 18 * Signature: ()V 19 * return: 子进程pid 20 */ 21 JNIEXPORT int JNICALL Java_main_activity_UninstalledObserverActivity_init(JNIEnv *env, jobject obj, jstring userSerial) 22 { 23 jstring tag = (*env)->NewStringUTF(env, TAG); 24 25 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &isCopy) 26 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "init observer"), &isCopy)); 27 28 // fork子进程,以执行轮询任务 29 pid_t pid = fork(); 30 if (pid < 0) 31 { 32 LOG_ERROR((*env)->GetStringUTFChars(env, tag, &isCopy) 33 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "fork failed !!!"), &isCopy)); 34 35 exit(1); 36 } 37 else if (pid == 0) 38 { 39 // 子进程注册监听器 40 int fileDescriptor = inotify_init(); 41 if (fileDescriptor < 0) 42 { 43 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &isCopy) 44 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "inotify_init failed !!!"), &isCopy)); 45 46 exit(1); 47 } 48 49 // 创建被监听文件,并加入监听 50 FILE *p_filesDir = fopen("/data/data/pym.test.uninstalledobserver/files", "r"); 51 if (p_filesDir == NULL) 52 { 53 int filesDirRet = mkdir("/data/data/pym.test.uninstalledobserver/files", S_IRWXU | S_IRWXG | S_IXOTH); 54 if (filesDirRet == -1) 55 { 56 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &isCopy) 57 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "inotify_add_watch failed !!!"), &isCopy)); 58 59 exit(1); 60 } 61 } 62 63 FILE *p_observedFile = fopen("/data/data/pym.test.uninstalledobserver/files/observedFile", "w"); 64 fclose(p_observedFile); 65 66 int watchDescriptor = inotify_add_watch(fileDescriptor, "/data/data/pym.test.uninstalledobserver/files/observedFile", IN_ALL_EVENTS); 67 if (watchDescriptor < 0) 68 { 69 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &isCopy) 70 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "inotify_add_watch failed !!!"), &isCopy)); 71 72 exit(1); 73 } 74 75 // 分配空间,以便读取event 76 void *p_buf = malloc(sizeof(struct inotify_event)); 77 if (p_buf == NULL) 78 { 79 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &isCopy) 80 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "malloc failed !!!"), &isCopy)); 81 82 exit(1); 83 } 84 // 分配空间,以便打印mask 85 int maskStrLength = 7 + 10 + 1;// mask=0x占7字节,32位整形数最大为10位,转换为字符串占10字节,'\0'占1字节 86 char *p_maskStr = malloc(maskStrLength); 87 if (p_maskStr == NULL) 88 { 89 free(p_buf); 90 91 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &isCopy) 92 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "malloc failed !!!"), &isCopy)); 93 94 exit(1); 95 } 96 97 // 开始监听 98 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &isCopy) 99 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "start observe"), &isCopy)); 100 101 while(1) 102 { 103 // read会阻塞进程 104 size_t readBytes = read(fileDescriptor, p_buf, sizeof(struct inotify_event)); 105 106 // 打印mask 107 snprintf(p_maskStr, maskStrLength, "mask=0x%x\0", ((struct inotify_event *) p_buf)->mask); 108 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &isCopy) 109 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, p_maskStr), &isCopy)); 110 111 // 若文件被删除,可能是已卸载,还需进一步判断父文件夹 112 if (IN_DELETE_SELF == ((struct inotify_event *) p_buf)->mask) 113 { 114 FILE *p_appDir = fopen("/data/data/pym.test.uninstalledobserver", "r"); 115 // 确认已卸载 116 if (p_appDir == NULL) 117 { 118 inotify_rm_watch(fileDescriptor, watchDescriptor); 119 120 break; 121 } 122 // 未卸载,可能用户执行了"清除数据" 123 else 124 { 125 fclose(p_appDir); 126 127 // 重新创建被监听文件,并重新监听 128 FILE *p_observedFile = fopen("/data/data/pym.test.uninstalledobserver/files/observedFile", "w"); 129 fclose(p_observedFile); 130 131 int watchDescriptor = inotify_add_watch(fileDescriptor, "/data/data/pym.test.uninstalledobserver/files/observedFile", IN_ALL_EVENTS); 132 if (watchDescriptor < 0) 133 { 134 free(p_buf); 135 free(p_maskStr); 136 137 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &isCopy) 138 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "inotify_add_watch failed !!!"), &isCopy)); 139 140 exit(1); 141 } 142 } 143 } 144 } 145 146 // 释放资源 147 free(p_buf); 148 free(p_maskStr); 149 150 // 停止监听 151 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &isCopy) 152 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "stop observe"), &isCopy)); 153 154 if (userSerial == NULL) 155 { 156 // 执行命令am start -a android.intent.action.VIEW -d http://www.baidu.com 157 execlp("am", "am", "start", "-a", "android.intent.action.VIEW", "-d", "http://www.baidu.com", (char *)NULL); 158 } 159 else 160 { 161 // 执行命令am start --user userSerial -a android.intent.action.VIEW -d http://www.baidu.com 162 execlp("am", "am", "start", "--user", (*env)->GetStringUTFChars(env, userSerial, &isCopy), "-a", "android.intent.action.VIEW", "-d", "http://www.baidu.com", (char *)NULL); 163 } 164 165 // 执行命令失败log 166 LOG_ERROR((*env)->GetStringUTFChars(env, tag, &isCopy) 167 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "exec AM command failed !!!"), &isCopy)); 168 } 169 else 170 { 171 // 父进程直接退出,使子进程被init进程领养,以避免子进程僵死,同时返回子进程pid 172 return pid; 173 } 174 } 175 176 #ifdef __cplusplus 177 } 178 #endif