Android应用开发中,app 卸载,有时会让用户给一些反馈意见,获取用户卸载app的原因,方便以后完善自己的产品,获取更多的用户量。
涉及技术 android jni开发 cmake,c++
应用卸载: 跳转道浏览器:execlp("am", "am", "start", "--user", "0", "-a","android.intent.action.VIEW", "-d", "http://www.baidu.com",(char*)NULL);
应用卸载: 安装应用: execlp("am", "am", "start", "--user", "0", "-n","com.android.packageinstaller/.PackageInstallerActivity", "-d","本地apk的路径",(char *) NULL)
am 对应android activitymanager
如果不懂命令 查看Android studio run 命令
#include
#define LOG_TAG "AppUninstallListener@native"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
using namespace std;
extern char *__progname;
extern "C"
//使用文件锁的形式判断
int need_fork_new_process_with_fl(string packageNameStr) {
string path ="/data/data/";
path+= packageNameStr;
path+= "/lib";
int fd = open(path.c_str(), O_RDONLY);
return flock(fd,LOCK_EX|LOCK_NB);
}
JNIEXPORT void JNICALL Java_com_uninstall_application_MainActivity_stringFromJNI(
JNIEnv *env, jobject obj, jstring packageName, jstring versionCode, jstring ttid,
jstring phoneBrand, jstring osVersion) {
const char * p_package = env->GetStringUTFChars(packageName, NULL);
string packageNameStr = p_package;
env->ReleaseStringUTFChars(packageName, p_package);
const char * p_version = env->GetStringUTFChars(versionCode, NULL);
string versionCodeStr = p_version;
env->ReleaseStringUTFChars(versionCode, p_version);
const char * p_ttid = env->GetStringUTFChars(ttid, NULL);
string ttidStr = p_ttid;
env->ReleaseStringUTFChars(ttid, p_ttid);
const char * p_phoneBrand = env->GetStringUTFChars(phoneBrand, NULL);
string phoneBrandStr = p_phoneBrand;
env->ReleaseStringUTFChars(phoneBrand, p_phoneBrand);
const char * p_osVersion = env->GetStringUTFChars(osVersion, NULL);
string osVersionStr = p_phoneBrand;
env->ReleaseStringUTFChars(osVersion, p_osVersion);
//if(!need_fork_new_process()) {
//LOGD("has exist a process on listening");
//return;
//}
if(need_fork_new_process_with_fl(packageNameStr) !=0) {
LOGD("file has locked by another process");
}
pid_t pid = fork();
if(pid <0) {
//进程fork失败
LOGD("fork process failed");
}else if(pid >0) {
//父进程中运行
}else {
//fork出的进程运行
LOGD("fork process success,current pid = %d", getpid());
sprintf(__progname, "uninstall_check");
//初始化inotify
int fd = inotify_init();
if (fd <0) {
LOGD("inotify_init failed");
exit(1);
}
string path ="/data/data/";
path+= packageNameStr;
path+= "/lib";
//LOGD("watching path:%s", path.c_str());
int wd = inotify_add_watch(fd, path.c_str(), IN_DELETE);
if (wd <0) {
LOGD("inotify_add_watch failed");
exit(1);
}
//分配缓存,以便读取event,缓存大小=一个struct inotify_event的大小,这样一次处理一个event
void *p_buf = malloc(sizeof(struct inotify_event));
if (p_buf ==NULL) {
LOGD("malloc failed !!!");
exit(1);
}
//开始监听
LOGD("start observer");
while(1) {
ssize_t readBytes = read(fd, p_buf,sizeof(struct inotify_event));
LOGD("read event happens");
//read会阻塞进程,走到这里说明收到目录被删除的事件
FILE *libDir = fopen(path.c_str(), "r");//判断该目录是否存在,如果存在则是覆盖安装
if(libDir ==NULL) {
// 应用被卸载了
LOGD("qapp uninstall,current version = %s, ttid = %s, phoneBrand = %s, osVersion = %s", versionCodeStr.c_str(), ttidStr.c_str(), phoneBrandStr.c_str(), osVersionStr.c_str());
if ((int)osVersion >=17) {
// Android4.2系统之后支持多用户操作,所以得指定用户
execlp("am", "am", "start", "--user", "0", "-a",
"android.intent.action.VIEW", "-d", "web url",
(char*)NULL);
}else {
// Android4.2以前的版本无需指定用户
execlp("am", "am", "start", "-a", "android.intent.action.VIEW",
"-d", "http://www.baidu.com", (char*)NULL);
}
}else {
// 覆盖安装
LOGD("app reinstall");
fclose(libDir);
free(p_buf);
p_buf =NULL;
exit(0);
}
}
if(p_buf !=NULL) {
free(p_buf);
}
}
}