一个应用被用户卸载肯定是有理由的,而开发者却未必能得知这一重要的理由,毕竟用户很少会主动反馈建议,多半就是用得不爽就卸,如果能在被卸载后获取到用户的一些反馈,那对开发者进一步改进应用是非常有利的。目前据我所知,国内的Android应用中实现这一功能的只有360手机卫士、360平板卫士,那么如何实现这一功能的?
我们可以把实现卸载反馈的问题转化为监听自己是否被卸载,只有得知自己被卸载,才可以设计相应的反馈处理流程。以下的列表是我在研究这一问题的思路:
1,注册BroadcastReceiver,监听"android.intent.action.PACKAGE_REMOVED"系统广播
结果:NO。未写代码,直接分析,卸载的第一步就是退出当前应用的主进程,而此广播是在已经卸载完成后才发出的,此时主进程都没有了,去哪onReceive()呢?
2,若能收到"将要卸载XX包"的系统广播,在主进程被退出之前就抢先进行反馈处理就好了,可惜没有这样的系统广播,不过经过调研,倒是发现了一个办法,读取系统log,当日志中包含"android.intent.action.DELETE"和自己的包名时,意味着自己将要被卸载。
结果:NO。调试时发现此方法有两个缺陷,(1)点击设置中的卸载按钮即发出此Intent,此时用户尚未在弹框中确认卸载;(2)pm命令卸载不出发此Intent,意味着被诸如手机安全管家,豌豆荚等软件卸载时,无法提前得知卸载意图。
3,由于时间点不容易把控,所以干脆不依赖系统广播或log,考虑到卸载过程会删除"/data/data/包名"目录,我们可以用线程直接轮询这个目录是否存在,以此为依据判断自己是否被卸载。
结果:NO。同方法1,主进程退出,相应的线程必定退出,线程还没等到判断目录是否存在就已经被销毁了。
4,改用C端进程轮询"/data/data/包名"目录是否存在
结果:YES。借助Java端进程fork出来的C端进程在应用被卸载后不会被销毁。
OK,上代码!
Activity启动时fork出C端进程轮询目录:
UninstalledMoniterActivity String TAG = "UninstalledMoniterActivity" Log.d(TAG, "load libuninstalled_moniter" System.loadLibrary("uninstalled_moniter" Log.d(TAG, "onCreate" }
核心——native方法头文件:
#include <jni.h> #include <stdio.h> #include <.h> #include <android/log.h> #include <unistd.h> MEM_ZERO(pDest, destSize) memset(pDest, 0, destSize) LOG_INFO(tag, msg) __android_log_write(ANDROID_LOG_INFO, tag, msg) LOG_DEBUG(tag, msg) __android_log_write(ANDROID_LOG_DEBUG, tag, msg) LOG_WARN(tag, msg) __android_log_write(ANDROID_LOG_WARN, tag, msg) LOG_ERROR(tag, msg) __android_log_write(ANDROID_LOG_ERROR, tag, msg) _Included_main_activity_UninstalledMoniterActivity main_activity_UninstalledMoniterActivity_MODE_PRIVATE main_activity_UninstalledMoniterActivity_MODE_PRIVATE 0L main_activity_UninstalledMoniterActivity_MODE_WORLD_READABLE main_activity_UninstalledMoniterActivity_MODE_WORLD_READABLE 1L main_activity_UninstalledMoniterActivity_MODE_WORLD_WRITEABLE main_activity_UninstalledMoniterActivity_MODE_WORLD_WRITEABLE 2L main_activity_UninstalledMoniterActivity_MODE_APPEND main_activity_UninstalledMoniterActivity_MODE_APPEND 32768L main_activity_UninstalledMoniterActivity_MODE_MULTI_PROCESS main_activity_UninstalledMoniterActivity_MODE_MULTI_PROCESS 4L main_activity_UninstalledMoniterActivity_BIND_AUTO_CREATE main_activity_UninstalledMoniterActivity_BIND_AUTO_CREATE 1L main_activity_UninstalledMoniterActivity_BIND_DEBUG_UNBIND main_activity_UninstalledMoniterActivity_BIND_DEBUG_UNBIND 2L main_activity_UninstalledMoniterActivity_BIND_NOT_FOREGROUND main_activity_UninstalledMoniterActivity_BIND_NOT_FOREGROUND 4L main_activity_UninstalledMoniterActivity_BIND_ABOVE_CLIENT main_activity_UninstalledMoniterActivity_BIND_ABOVE_CLIENT 8L main_activity_UninstalledMoniterActivity_BIND_ALLOW_OOM_MANAGEMENT main_activity_UninstalledMoniterActivity_BIND_ALLOW_OOM_MANAGEMENT 16L main_activity_UninstalledMoniterActivity_BIND_WAIVE_PRIORITY main_activity_UninstalledMoniterActivity_BIND_WAIVE_PRIORITY 32L main_activity_UninstalledMoniterActivity_BIND_IMPORTANT main_activity_UninstalledMoniterActivity_BIND_IMPORTANT 64L main_activity_UninstalledMoniterActivity_BIND_ADJUST_WITH_ACTIVITY main_activity_UninstalledMoniterActivity_BIND_ADJUST_WITH_ACTIVITY 128L main_activity_UninstalledMoniterActivity_CONTEXT_INCLUDE_CODE main_activity_UninstalledMoniterActivity_CONTEXT_INCLUDE_CODE 1L main_activity_UninstalledMoniterActivity_CONTEXT_IGNORE_SECURITY main_activity_UninstalledMoniterActivity_CONTEXT_IGNORE_SECURITY 2L main_activity_UninstalledMoniterActivity_CONTEXT_RESTRICTED main_activity_UninstalledMoniterActivity_CONTEXT_RESTRICTED 4L main_activity_UninstalledMoniterActivity_RESULT_CANCELED main_activity_UninstalledMoniterActivity_RESULT_CANCELED 0L main_activity_UninstalledMoniterActivity_RESULT_OK main_activity_UninstalledMoniterActivity_RESULT_OK -1L main_activity_UninstalledMoniterActivity_RESULT_FIRST_USER main_activity_UninstalledMoniterActivity_RESULT_FIRST_USER 1L main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DISABLE main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DISABLE 0L main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DIALER main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DIALER 1L main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SHORTCUT main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SHORTCUT 2L main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_LOCAL main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_GLOBAL main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L JNIEXPORT JNICALL Java_main_activity_UninstalledMoniterActivity_init(JNIEnv *
核心——native方法实现:
#include c_TAG[] = jboolean b_IS_COPY = JNIEXPORT JNICALL Java_main_activity_UninstalledMoniterActivity_init(JNIEnv * jstring tag = (*env)-> LOG_DEBUG((*env)->GetStringUTFChars(env, tag, & , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, ), & pid_t pid = (pid < LOG_ERROR((*env)->GetStringUTFChars(env, tag, & , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, ), & (pid == ( FILE *p_file = fopen(, (p_file != LOG_DEBUG((*env)->GetStringUTFChars(env, tag, & , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, ), & sleep( LOG_DEBUG((*env)->GetStringUTFChars(env, tag, & , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, ), & execlp(, , , , , , , ( *
注1:为了调试方便,包含<android/log.h>,使得so在执行过程中也可以像Java端一样方便得打出log。相应的mk文件需要加上以下两句声明
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog
注2:代码中引用了360手机卫士的反馈地址,仅供大家学习、测试使用~~~