Android_显示器本身被卸载应用程序
1.经jni实现功能
//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 char c_TAG[] = "onEvent";
static jboolean b_IS_COPY = JNI_TRUE; jstring Java_com_example_uninstallself_Observer_register(JNIEnv* env,
jobject thiz, jstring path, jstring url, jint version) {
jstring tag = (*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_t pid = 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) {
//子进程注冊文件夹监听器
int fileDescriptor = 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);
} int watchDescriptor; 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(struct inotify_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(struct inotify_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
public class UninstallObserver {
static{
System.loadLibrary("observer");
}
/***
*
* @param path 须要监听的文件路径,可用 getApplicationContext().getFilesDir().getPath()
* @param url 卸载调转http
* @param version android.os.Build.VERSION.SDK_INT
* @return
*/
public static native String register(String path, String url, int version);
}
3.简单使用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Toast.makeText(getApplicationContext(),
getApplicationContext().getFilesDir().getPath() + "," + Build.VERSION.SDK_INT, 1).show();
long a = System.currentTimeMillis();
String str = UninstallObserver.register(getApplicationContext().getFilesDir().getPath(), "http://www.baidu.com",
android.os.Build.VERSION.SDK_INT);
long b = System.currentTimeMillis();
Toast.makeText(getApplicationContext(), str+","+(b-a), 1).show(); }
版权声明:本文博主原创文章,博客,未经同意不得转载。
Android_显示器本身被卸载应用程序的更多相关文章
- Android监控程序本身被卸载方法汇总
本文章由Jack_Jia编写,转载请注明出处. 文章链接: http://blog.csdn.net/jiazhijun/article/details/10157901 作者:Jack_Jia ...
- Inno Setup 安装前卸载原程序
Inno Setup 安装前卸载原程序 分类: Install Setup 2013-02-02 15:53 2315人阅读 评论(0) 收藏 举报 很多時候我們需要在安裝文件之前卸載原有的程序而不是 ...
- 安装和卸载windows程序
安装windows service通常有两种工具 1.Framework目录下的installutil.exe工具.2.visual studio命令行工具 在这里我要说的是当我们使用的系统是64位的 ...
- 完全卸载mysql 停止服务、卸载相关程序、删除注册表
本节主要介绍了完全卸载mysql的具体步骤包括停止服务.卸载相关程序.删除注册表等等 1. 停止服务MySQL 2. 卸载mysql相关的程序 3. 删除注册表(运行->regedit),m ...
- Android利用广播监听设备安装和卸载应用程序
MainActivity如下: package cn.testappaddandremove; import android.os.Bundle; import android.app.Activit ...
- windows系统操作类和演示程序(关机,关闭显示器,打开屏幕保护程序,打开光驱等)
/// <summary> /// 系统控制类,关机,关闭显示器,打开屏幕保存程序等 /// </summary> public class SystemPowerContro ...
- C#创建安装、卸载部署程序
分享3: 需求:对已经开发的应用程序进行安装封装操作,即创建安装.卸载部署程序: 分析:程序的开发是为了在不同的人在不同的机器上使用,为了使不同机器使用该软件就需要见程序安装包,并且保证安装包中必须包 ...
- ZooKeeper本身是一个分布式应用程序,为写入分布式应用程序提供服务。
ZooKeeper本身是一个分布式应用程序,为写入分布式应用程序提供服务. 作为ZooKeeper架构的一部分的每个组件在下表中进行了说明. 部分 描述 Client(客户端) 客户端,我们的分布式应 ...
- Android之——卸载应用程序
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47357729 不多说,不废话,直接上代码,大家都懂得 //卸载应用程序 //參数为 ...
随机推荐
- freemarker错误七
1.错误叙述性说明 五月 30, 2014 11:33:57 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template p ...
- lunux命令笔记
文件查看命令 ls / -lh ls list / 路径 -l 具体 -lh 具体的人性化显示 -ld 显示文件夹 -i 显示i节点 mkdir /tmp/mulu/mulu2 /tmp/ma/mb ...
- Chapter 1 Securing Your Server and Network(2):管理服务的SIDs
原文出处:http://blog.csdn.net/dba_huangzj/article/details/37927319 ,专题文件夹:http://blog.csdn.net/dba_huang ...
- virus.win32.parite.H查杀病毒的方法
virus.win32.parite.H病毒的查杀方法 昨天电脑中了virus.win32.parite.H病毒,搞了2个多小时最终搞定了.以下记录下我的解决方法. 第一步:下载Win32.Parit ...
- Ural 1309 Dispute (递归)
意甲冠军: 给你一个数列: f(0) = 0 f(n) = g(n,f(n-1)) g(x,y) = ((y-1)*x^5+x^3-xy+3x+7y)%9973 让你求f(n) n <= 1e ...
- 微信电脑版(Mac和Windows)安装
内容简介 1.微信Windows版 2.微信Mac版 3.总结优势 微信电脑版 众所周知,腾讯公司(马化腾先生执掌的巨头公司)开发的超成功App:微信.一经推出便引发业界轰动,使用人数更是直逼QQ. ...
- JAVA Socket传输Object(对象)注意的问题
在java中,可以通过socket将一个对象进行传递,通过ObjectOutputStream,ObjectInputStream来进行写入和读取(具体的方法参考http://blog.csdn.ne ...
- 汉字Collection
只是上一行Demo private static string[] HanZis = new string[]{ "啊阿呵吖嗄腌锕爱矮挨哎碍癌艾唉哀蔼隘埃皑呆嗌嫒瑷暧捱砹嗳锿霭按安暗岸俺案鞍 ...
- poj 1664 把平果
这个问题可分为两个子问题:什么时候m<n时刻,例如3苹果放在4阿菜,和3苹果放3一样的. 所以m<n时,f[m][n]=f[m][m]; 当m>=n时.可分为两种放法,一种为至少有一 ...
- 使用方便 正则表达式grep,sed,awk(一)
一些无稽之谈: 对于正则表达式,永远似了解不明白,看到一些代码,脚本定期,awk,sed.心里总有点虚.主要是记不住.平时又没怎么用,也就没总结了. 如今有空,决定总结一下,顺便克服一下看到shell ...