Android应用如何监听自己是否被卸载及卸载反馈功能的实现

  一个应用被用户卸载肯定是有理由的,而开发者却未必能得知这一重要的理由,毕竟用户很少会主动反馈建议,多半就是用得不爽就卸,如果能在被卸载后获取到用户的一些反馈,那对开发者进一步改进应用是非常有利的。目前据我所知,国内的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端进程轮询目录:

 package main.activity;

 import pym.test.uninstalledmoniter.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log; /**
* @author pengyiming
* @note 监听此应用是否被卸载,若被卸载则弹出卸载反馈
*
*/ public class UninstalledMoniterActivity extends Activity
{
/* 数据段begin */
private static final String TAG = "UninstalledMoniterActivity";
/* 数据段end */ /* 函数段begin */
private native void init();
static
{
Log.d(TAG, "load libuninstalled_moniter");
System.loadLibrary("uninstalled_moniter");
} @Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate"); setContentView(R.layout.uninstalled_moniter_layout); init();
}
/* 函数段end */
}

核心——native方法头文件:

 /* 头文件begin */
#include <jni.h>
#include <stdio.h>
#include <string.h>
#include <android/log.h>
#include <unistd.h>
/* 头文件end */ /* 宏定义begin */
//清0宏
#define MEM_ZERO(pDest, destSize) memset(pDest, 0, destSize) //LOG宏定义
#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)
/* 宏定义end */ #ifndef _Included_main_activity_UninstalledMoniterActivity
#define _Included_main_activity_UninstalledMoniterActivity
#ifdef __cplusplus
extern "C" {
#endif #undef main_activity_UninstalledMoniterActivity_MODE_PRIVATE
#define main_activity_UninstalledMoniterActivity_MODE_PRIVATE 0L
#undef main_activity_UninstalledMoniterActivity_MODE_WORLD_READABLE
#define main_activity_UninstalledMoniterActivity_MODE_WORLD_READABLE 1L
#undef main_activity_UninstalledMoniterActivity_MODE_WORLD_WRITEABLE
#define main_activity_UninstalledMoniterActivity_MODE_WORLD_WRITEABLE 2L
#undef main_activity_UninstalledMoniterActivity_MODE_APPEND
#define main_activity_UninstalledMoniterActivity_MODE_APPEND 32768L
#undef main_activity_UninstalledMoniterActivity_MODE_MULTI_PROCESS
#define main_activity_UninstalledMoniterActivity_MODE_MULTI_PROCESS 4L
#undef main_activity_UninstalledMoniterActivity_BIND_AUTO_CREATE
#define main_activity_UninstalledMoniterActivity_BIND_AUTO_CREATE 1L
#undef main_activity_UninstalledMoniterActivity_BIND_DEBUG_UNBIND
#define main_activity_UninstalledMoniterActivity_BIND_DEBUG_UNBIND 2L
#undef main_activity_UninstalledMoniterActivity_BIND_NOT_FOREGROUND
#define main_activity_UninstalledMoniterActivity_BIND_NOT_FOREGROUND 4L
#undef main_activity_UninstalledMoniterActivity_BIND_ABOVE_CLIENT
#define main_activity_UninstalledMoniterActivity_BIND_ABOVE_CLIENT 8L
#undef main_activity_UninstalledMoniterActivity_BIND_ALLOW_OOM_MANAGEMENT
#define main_activity_UninstalledMoniterActivity_BIND_ALLOW_OOM_MANAGEMENT 16L
#undef main_activity_UninstalledMoniterActivity_BIND_WAIVE_PRIORITY
#define main_activity_UninstalledMoniterActivity_BIND_WAIVE_PRIORITY 32L
#undef main_activity_UninstalledMoniterActivity_BIND_IMPORTANT
#define main_activity_UninstalledMoniterActivity_BIND_IMPORTANT 64L
#undef main_activity_UninstalledMoniterActivity_BIND_ADJUST_WITH_ACTIVITY
#define main_activity_UninstalledMoniterActivity_BIND_ADJUST_WITH_ACTIVITY 128L
#undef main_activity_UninstalledMoniterActivity_CONTEXT_INCLUDE_CODE
#define main_activity_UninstalledMoniterActivity_CONTEXT_INCLUDE_CODE 1L
#undef main_activity_UninstalledMoniterActivity_CONTEXT_IGNORE_SECURITY
#define main_activity_UninstalledMoniterActivity_CONTEXT_IGNORE_SECURITY 2L
#undef main_activity_UninstalledMoniterActivity_CONTEXT_RESTRICTED
#define main_activity_UninstalledMoniterActivity_CONTEXT_RESTRICTED 4L
#undef main_activity_UninstalledMoniterActivity_RESULT_CANCELED
#define main_activity_UninstalledMoniterActivity_RESULT_CANCELED 0L
#undef main_activity_UninstalledMoniterActivity_RESULT_OK
#define main_activity_UninstalledMoniterActivity_RESULT_OK -1L
#undef main_activity_UninstalledMoniterActivity_RESULT_FIRST_USER
#define main_activity_UninstalledMoniterActivity_RESULT_FIRST_USER 1L
#undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DISABLE
#define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DISABLE 0L
#undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DIALER
#define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DIALER 1L
#undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SHORTCUT
#define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SHORTCUT 2L
#undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_LOCAL
#define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L
#undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_GLOBAL
#define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L /*
* Class: main_activity_UninstalledMoniterActivity
* Method: init
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_main_activity_UninstalledMoniterActivity_init(JNIEnv *, jobject); #ifdef __cplusplus
}
#endif
#endif

核心——native方法实现:

 /* 头文件begin */
#include "main_activity_UninstalledMoniterActivity.h"
/* 头文件end */ #ifdef __cplusplus
extern "C" {
#endif /* 内全局变量begin */
static char c_TAG[] = "UninstalledMoniterActivity.init";
static jboolean b_IS_COPY = JNI_TRUE;
/* 内全局变量 */ /*
* Class: main_activity_UninstalledMoniterActivity
* Method: init
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_main_activity_UninstalledMoniterActivity_init(JNIEnv *env, jobject obj)
{
jstring tag = (*env)->NewStringUTF(env, c_TAG); //初始化log
LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY)
, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "init OK"), &b_IS_COPY)); //fork子进程,以执行轮询任务
pid_t pid = fork();
if (pid < )
{
//出错log
LOG_ERROR((*env)->GetStringUTFChars(env, tag, &b_IS_COPY)
, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "fork error !!!"), &b_IS_COPY));
}
else if (pid == )
{
//子进程轮询"/data/data/pym.test.uninstalledmoniter"目录是否存在,若不存在则说明已被卸载
while ()
{
FILE *p_file = fopen("/data/data/pym.test.uninstalledmoniter", "r");
if (p_file != NULL)
{
fclose(p_file); //目录存在log
LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY)
, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "I'm OK !!!"), &b_IS_COPY)); sleep();
}
else
{
//目录不存在log
LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY)
, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "I'm NOT OK !!!"), &b_IS_COPY)); //执行命令am start -a android.intent.action.VIEW -d http://shouji.360.cn/web/uninstall/uninstall.html
execlp("am", "am", "start", "-a", "android.intent.action.VIEW", "-d", "http://shouji.360.cn/web/uninstall/uninstall.html", (char *)NULL);
}
}
}
else
{
//父进程直接退出,使子进程被init进程领养,以避免子进程僵死
}
} #ifdef __cplusplus
}
#endif

注1:为了调试方便,包含<android/log.h>,使得so在执行过程中也可以像Java端一样方便得打出log。相应的mk文件需要加上以下两句声明

LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog

注2:代码中引用了360手机卫士的反馈地址,仅供大家学习、测试使用~~~

上一篇:org.eclipse.jdi.TimeoutException: Timeout occurred while waiting for packet 421. occured resuming VM.


下一篇:Cookie的基本使用