Android研究之監聽自身應用被卸載代碼實現

來源:互聯網
上載者:User

Android研究之監聽自身應用被卸載代碼實現

1.通過jni實現函數


123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 //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) /* 內全域變數begin */static charc_TAG[]= "onEvent";staticjboolean b_IS_COPY= JNI_TRUE; jstringJava_com_example_uninstallself_Observer_register(JNIEnv*env, jobject thiz,jstring path,jstring url,jint version){ jstringtag =(*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_tpid =fork(); if(pid< 0){ //出錯log LOG_ERROR((*env)->GetStringUTFChars(env,tag,&b_IS_COPY), (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env,"fork failed !!!"), &b_IS_COPY)); }else if(pid== 0){ //子進程註冊目錄監聽器 intfileDescriptor = inotify_init(); if(fileDescriptor< 0){ LOG_DEBUG((*env)->GetStringUTFChars(env,tag,&b_IS_COPY), (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "inotify_init failed !!!"),&b_IS_COPY)); exit(1); } intwatchDescriptor; watchDescriptor= inotify_add_watch(fileDescriptor, (*env)->GetStringUTFChars(env,path,NULL),IN_DELETE); if(watchDescriptor< 0){ LOG_DEBUG((*env)->GetStringUTFChars(env,tag,&b_IS_COPY), (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "inotify_add_watch failed !!!"), &b_IS_COPY)); exit(1); } //分配緩衝,以便讀取event,緩衝大小=一個struct inotify_event的大小,這樣一次處理一個event void*p_buf= malloc(sizeof(structinotify_event)); if(p_buf== NULL){ LOG_DEBUG((*env)->GetStringUTFChars(env,tag,&b_IS_COPY), (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env,"malloc failed !!!"), &b_IS_COPY)); exit(1); } //開始監聽 LOG_DEBUG((*env)->GetStringUTFChars(env,tag,&b_IS_COPY), (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env,"start observer"), &b_IS_COPY)); //read會阻塞進程, size_t readBytes= read(fileDescriptor,p_buf, sizeof(structinotify_event)); //走到這裡說明收到目錄被刪除的事件,登出監聽器 free(p_buf); inotify_rm_watch(fileDescriptor,IN_DELETE); //目錄不存在log LOG_DEBUG((*env)->GetStringUTFChars(env,tag,&b_IS_COPY), (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env,"uninstalled"),&b_IS_COPY)); if(version>= 17){ //4.2以上的系統由於使用者權限管理更嚴格,需要加上 --user 0 execlp("am","am","start","--user","0","-a", "android.intent.action.VIEW","-d", (*env)->GetStringUTFChars(env,url,NULL),(char*)NULL); }else { execlp("am","am","start","-a","android.intent.action.VIEW", "-d",(*env)->GetStringUTFChars(env,url,NULL), (char*)NULL); } //擴充:可以執行其他shell命令,am(即activity manager),可以開啟某程式、服務,broadcast intent,等等 }else { //父進程直接退出,使子進程被init進程領養,以避免子進程僵死 } return(*env)->NewStringUTF(env,"Hello from JNI !");}

2.定義UninstallObserver


1234567891011121314 publicclass UninstallObserver{ static{ System.loadLibrary("observer"); } /*** * * @param path 需要監聽的檔案路徑,可用 getApplicationContext().getFilesDir().getPath() * @param url 卸載調轉http * @param version android.os.Build.VERSION.SDK_INT * @return */ publicstatic nativeString register(Stringpath,String url,int version);}

3.簡單使用


1234567891011121314 @Overrideprotected voidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toast.makeText(getApplicationContext(), getApplicationContext().getFilesDir().getPath()+ ","+ Build.VERSION.SDK_INT,1).show(); longa =System.currentTimeMillis(); Stringstr =UninstallObserver.register(getApplicationContext().getFilesDir().getPath(),"http://www.baidu.com", android.os.Build.VERSION.SDK_INT); longb =System.currentTimeMillis(); Toast.makeText(getApplicationContext(),str+","+(b-a),1).show(); }


本文參考資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.