Demo地址:https://github.com/wqycsu/uninstalldemo
注:Android 5.0以上主進(jìn)程被殺后生蚁,fork出的進(jìn)程也會(huì)被殺,所以無(wú)法實(shí)現(xiàn)監(jiān)聽(tīng)
最近項(xiàng)目要增加卸載統(tǒng)計(jì)瘪贱,用戶卸載時(shí)android層的代碼已經(jīng)沒(méi)作用了锚烦,所以傳統(tǒng)的打點(diǎn)統(tǒng)計(jì)方式也無(wú)法發(fā)揮作用已脓。在網(wǎng)上搜了下目前已有的實(shí)現(xiàn)方式,基本都是使用linux的inotify_add_watch
袜漩,方法詳細(xì)介紹可以看這里绪爸,大概思路就是使用該方法監(jiān)聽(tīng)某一個(gè)文件,如果該文件被刪除就可以認(rèn)為App被卸載了宙攻,思路有了奠货,那怎么實(shí)現(xiàn)呢,接下來(lái)便一一解答座掘。递惋?這里有幾點(diǎn)需要注意:1、如何監(jiān)聽(tīng)溢陪?2萍虽、監(jiān)聽(tīng)那個(gè)文件?3形真、如何避免重復(fù)監(jiān)聽(tīng)杉编?
如何監(jiān)聽(tīng)
App被卸載時(shí)主進(jìn)程肯定是靠不住了,那自然就需要另一個(gè)獨(dú)立的進(jìn)程實(shí)現(xiàn)監(jiān)聽(tīng)咆霜,所以就需要使用fork()
了邓馒,關(guān)于fork()
,這篇博客講解很清楚蛾坯。fork()
調(diào)用一次光酣,能夠返回兩次,它有三種可能的返回值:
1偿衰、在父進(jìn)程中挂疆,fork()
返回新創(chuàng)建子進(jìn)程的進(jìn)程ID;
2下翎、在子進(jìn)程中宝当,fork()
返回0;
3庆揩、如果出現(xiàn)錯(cuò)誤跌穗,fork()
返回一個(gè)負(fù)值虏辫;
所以通過(guò)返回值我們可以知道那個(gè)是子進(jìn)程,然后在子進(jìn)程做需要做的事情砌庄,在這里要做的就是監(jiān)聽(tīng)文件了。那監(jiān)聽(tīng)那個(gè)文件呢佩微?
監(jiān)聽(tīng)文件選擇
關(guān)于監(jiān)聽(tīng)那個(gè)文件萌焰,我也在網(wǎng)上查找了資料,大家的方案基本上是兩種(使用的監(jiān)聽(tīng)方法都是inotify_add_watch(fd, dir, IN_DELETE)
):
1扒俯、監(jiān)聽(tīng)/data/data/packageName/
目錄
2、自己創(chuàng)建一個(gè)文件夺姑,然后監(jiān)聽(tīng)自己創(chuàng)建的文件
使用這種監(jiān)聽(tīng)方式時(shí)互纯,第一種方案就暴露出了不足,因?yàn)?code>IN_DELETE是監(jiān)聽(tīng)被監(jiān)聽(tīng)目錄下的文件或目錄被刪除留潦,所以它無(wú)法區(qū)分是清數(shù)據(jù)還是卸載。這樣第二種監(jiān)聽(tīng)方案就出現(xiàn)了兔院,但是在/data/data/packageName/
目錄下創(chuàng)建文件的權(quán)限系統(tǒng)是否給予,這點(diǎn)我沒(méi)去驗(yàn)證孵稽。所以我還是選擇了第一種方案十偶,但監(jiān)聽(tīng)方法的參數(shù)我修改為IN_DELETE_SELF
,因?yàn)樵搮?shù)標(biāo)識(shí)只有被監(jiān)聽(tīng)的文件或目錄本身被刪除時(shí)才會(huì)觸發(fā)notify事件惦积。
如何避免重復(fù)監(jiān)聽(tīng)
監(jiān)聽(tīng)方式已經(jīng)有了,但還是有問(wèn)題的蛛勉,那就是我們不可能每次打開(kāi)App都fork一個(gè)進(jìn)程出來(lái)鹿寻,這顯然是不合理的诽凌,那如何判斷監(jiān)聽(tīng)進(jìn)程已經(jīng)存在了呢?我想到了兩種方案:
1痢法、通過(guò)Socket端口綁定判斷
進(jìn)程每一次創(chuàng)建時(shí)會(huì)先嘗試綁定一個(gè)端口窝趣,如果綁定成功則說(shuō)明監(jiān)聽(tīng)進(jìn)程還沒(méi)創(chuàng)建或已經(jīng)dead疯暑,此時(shí)則fork進(jìn)程哑舒。否則就說(shuō)明監(jiān)聽(tīng)進(jìn)程已經(jīng)存在,代碼如下:
//使用綁定socket形式判斷進(jìn)程是否存在,有風(fēng)險(xiǎn)
int need_fork_new_process() {
//create socket
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd==-1)
{
LOGD("create socket success");
return 1;
}
//build connection address
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(9876);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
int r = -2;
r = bind(fd,(struct sockaddr*)&addr,sizeof(addr));
if(r == -1)
{
LOGD("bind socket failed");
close(fd);
return 0;
} else if(r == 0) {
LOGD("bind socket success");
return 1;
}
return 1;
}
2越锈、使用文件鎖判斷
同樣膘滨,進(jìn)程每次fork前先嘗試對(duì)某個(gè)文件進(jìn)行加鎖,如果加鎖成功則說(shuō)明監(jiān)聽(tīng)進(jìn)程存在火邓,否則則fork新的進(jìn)程。代碼如下:
//使用文件鎖的形式判斷
int need_fork_new_process_with_fl(string packageNameStr) {
string path = "/data/data/";
path += packageNameStr;
path += "/lib";//對(duì)app的libs目錄加鎖
int fd = open(path.c_str(), O_RDONLY);
return flock(fd,LOCK_EX|LOCK_NB);
}
在具體實(shí)現(xiàn)時(shí)我選擇了第二種方案躲胳,因?yàn)镾ocket端口號(hào)綁定時(shí)纤勒,如果選擇的端口已經(jīng)被綁定坯苹,我無(wú)法100%保證它是被我創(chuàng)建的進(jìn)程綁定的摇天。
OK,到此方案雛形已經(jīng)有了泉坐,接下來(lái)就看代碼如何實(shí)現(xiàn)了:
JNIEXPORT void JNICALL Java_com_alibaba_wxlib_util_AppUninstallListener_uninstallListen
(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("文件已經(jīng)被一個(gè)進(jìn)程加鎖了");
return;
}
pid_t pid = fork();
if(pid < 0) {
//進(jìn)程fork失敗
LOGD("進(jìn)程fork失敗");
} else if(pid > 0) {
//父進(jìn)程中運(yùn)行
} else {
//fork出的進(jìn)程運(yùn)行
LOGD("fork進(jìn)程成功,pid = %d", getpid());
//初始化inotify
int fd = inotify_init();
if (fd < 0) {
LOGD("inotify_init失敗");
exit(1);
}
string path = "/data/data/";
path += packageNameStr;
LOGD("監(jiān)聽(tīng)目錄:%s", path.c_str());
int wd = inotify_add_watch(fd, path.c_str(), IN_DELETE_SELF);
if (wd < 0) {
LOGD("inotify_add_watch失敗");
exit(1);
}
//分配緩存讀取event
void *p_buf = malloc(sizeof(struct inotify_event));
if (p_buf == NULL) {
LOGD("分配緩存失敗!");
exit(1);
}
//開(kāi)始監(jiān)聽(tīng)
//LOGD("開(kāi)始監(jiān)聽(tīng)");
while(1) {
ssize_t readBytes = read(fd, p_buf,sizeof(struct inotify_event));
//read是阻塞式的腕让,運(yùn)行到這里目錄被刪除了
FILE *libDir = fopen(path.c_str(), "r");//再次判斷該目錄是否存在,如果存在則是覆蓋安裝
if(libDir == NULL) {
// 應(yīng)用被卸載了
LOGD("應(yīng)用被卸載,version = %s, ttid = %s, phoneBrand = %s, osVersion = %s", versionCodeStr.c_str(), ttidStr.c_str(), phoneBrandStr.c_str(), osVersionStr.c_str());
inotify_rm_watch(fd, IN_DELETE);//移除監(jiān)聽(tīng)
free(p_buf);
p_buf = NULL;
//向服務(wù)端發(fā)送app卸載事件
int ret = sendUninstallInfoToServer(packageNameStr, versionCodeStr, ttidStr, phoneBrandStr, osVersionStr);
if(ret) {
LOGD("發(fā)送成功!");
} else {
LOGD("發(fā)送失敗!");
}
//工作結(jié)束记某,退出進(jìn)程
exit(0);
} else {
// 覆蓋安裝
LOGD("覆蓋安裝了");
fclose(libDir);
free(p_buf);
p_buf = NULL;
//退出進(jìn)程司训,因?yàn)樾枰匦卤O(jiān)聽(tīng)新安裝的版本了
exit(0);
}
}
if(p_buf != NULL) {
free(p_buf);
}
}
}
本文結(jié)束液南,經(jīng)過(guò)這個(gè)功能的實(shí)現(xiàn),收獲良多滑凉。