android点击跳转卸载,android 监听应用卸载,跳转到浏览器或安装应用

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 命令

0d50cf967670

#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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值