卸载app事件监听

来源:互联网 发布:淘宝琴行哪个好 编辑:程序博客网 时间:2024/05/21 15:26

360手机助手有一项有意思的功能,卸载后会跳到浏览器,然后弹出卸载调查界面,那么这个是怎么实现的呢?


首先系统是不提供这个监听事件的,所以我们只能自己想办法了。有人说用fileobserver来监听文件夹变化,本人亲测没有成功,可能是只能在部分机型里面适用。后面在一个叫终端研发部的公众号中得到了提示,发现卸载的时候native进程还会继续存在,并且可以做相关操作,那么为什么不用c语言写一个事件呢?

1.在main下新建cpp文件夹,新建一个.cpp文件,写好卸载以后的实现,比如

#include <jni.h>#include <string>#include <android/log.h>#include <sys/inotify.h>#define LOG_TAG "yuyahao1"#include <unistd.h>#include <malloc.h>#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)#define LOGF(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)extern "C"{JNIEXPORT jstring JNICALLJava_unstall_yyh_com_a360installtolistener_MainActivity_stringFromJNI(        JNIEnv *env,        jobject /* this */) {    std::string hello = "欢迎关注终端研发部:<br><br><br>"            "ndk项目实战—高仿360手机助手之卸载监听";    LOGD(LOG_TAG,"我进入到这个减小的局面了");    return env->NewStringUTF(hello.c_str());}JNIEXPORTvoid JNICALLJava_unstall_yyh_com_a360installtolistener_MainActivity_callUnInstallListener(JNIEnv *env,jobject obj,jint versionSdk,jstring path){    LOGD("------------------------");    LOGF("------------------------");    const char * path_str = env->GetStringUTFChars(path,0);        pid_t pid =  fork();        if(pid < 0){               LOGD("克隆失败");        }else if(pid > 0){            LOGD("父进程");        }else{            LOGD("子进程!");            //*******************在这里进程操作*****************            LOGD("你好,终端研发部");            int fuileDescript = inotify_init();            int watch = inotify_add_watch(fuileDescript,path_str,IN_DELETE_SELF);           void * p =  malloc(sizeof(struct inotify_event));            read(fuileDescript,p, sizeof(struct inotify_event));            inotify_rm_watch(fuileDescript,watch);            LOGD(LOG_TAG,"接下来进行操作,来条状网页!!!");            if(versionSdk< 17){                //am start -a android.intent.action.VIEW -d  http://gityuan.com                execlp("am","am","start","-a","android.intent.action.VIEW","-d","http://www.baidu.com",NULL);            }else{                execlp("am","am","start","--user","0","-a","android.intent.action.VIEW","-d", "http://www.fanli.com",NULL);            }        }        env->ReleaseStringUTFChars(path,path_str);    }}
2.在使用的activity中声明这个native方法

private native void callUnInstallListener(int versionSdk,String path);
3.调用该方法,比如oncreate中也可以

callUnInstallListener(Build.VERSION.SDK_INT,"data/data/unstall.yyh.com.a360installtolistener");
4.清单文件中添加相关权限

<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

原创粉丝点击