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_显示器本身被卸载应用程序的更多相关文章

  1. Android监控程序本身被卸载方法汇总

    本文章由Jack_Jia编写,转载请注明出处.   文章链接: http://blog.csdn.net/jiazhijun/article/details/10157901 作者:Jack_Jia ...

  2. Inno Setup 安装前卸载原程序

    Inno Setup 安装前卸载原程序 分类: Install Setup 2013-02-02 15:53 2315人阅读 评论(0) 收藏 举报 很多時候我們需要在安裝文件之前卸載原有的程序而不是 ...

  3. 安装和卸载windows程序

    安装windows service通常有两种工具 1.Framework目录下的installutil.exe工具.2.visual studio命令行工具 在这里我要说的是当我们使用的系统是64位的 ...

  4. 完全卸载mysql 停止服务、卸载相关程序、删除注册表

    本节主要介绍了完全卸载mysql的具体步骤包括停止服务.卸载相关程序.删除注册表等等   1. 停止服务MySQL 2. 卸载mysql相关的程序 3. 删除注册表(运行->regedit),m ...

  5. Android利用广播监听设备安装和卸载应用程序

    MainActivity如下: package cn.testappaddandremove; import android.os.Bundle; import android.app.Activit ...

  6. windows系统操作类和演示程序(关机,关闭显示器,打开屏幕保护程序,打开光驱等)

    /// <summary> /// 系统控制类,关机,关闭显示器,打开屏幕保存程序等 /// </summary> public class SystemPowerContro ...

  7. C#创建安装、卸载部署程序

    分享3: 需求:对已经开发的应用程序进行安装封装操作,即创建安装.卸载部署程序: 分析:程序的开发是为了在不同的人在不同的机器上使用,为了使不同机器使用该软件就需要见程序安装包,并且保证安装包中必须包 ...

  8. ZooKeeper本身是一个分布式应用程序,为写入分布式应用程序提供服务。

    ZooKeeper本身是一个分布式应用程序,为写入分布式应用程序提供服务. 作为ZooKeeper架构的一部分的每个组件在下表中进行了说明. 部分 描述 Client(客户端) 客户端,我们的分布式应 ...

  9. Android之——卸载应用程序

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47357729 不多说,不废话,直接上代码,大家都懂得 //卸载应用程序 //參数为 ...

随机推荐

  1. 我只是不甘心-------Day51

    回放假回家一天,完全断网,天气也很给力配合.水蓝色的天空.白云,抬眼,我没有看到刺目的光芒,但仍眼眼睛刺痛.已经缩小眼,我试图打开眼睛,就像眼泪都流出来了,它不会擦到沙,这是很多其他的没地方. 哥哥去 ...

  2. POJ 2155 D区段树

    POJ 2155  D区段树 思考:D区段树是每个节点设置一个段树树. 刚開始由于题目是求A[I,J],然后在y查询那直接ans^=Map[i][j]的时候没看懂.后面自己把图画出来了才理解. 由于仅 ...

  3. hdu 4864 Task (馋)

    # include <stdio.h> # include <algorithm> # include <string.h> using namespace std ...

  4. POJ3623:Best Cow Line, Gold(后缀数组)

    Description FJ is about to take his N (1 ≤ N ≤ 30,000) cows to the annual"Farmer of the Year&qu ...

  5. Python调用微博API

    上头叫通过微博ID获取用户公布过的历史微博内容,于是研究了下新浪微博提供的API 1 首先在微博开放中心下"创建应用"创建一个应用,应用信息那些随便填,填写完成后,不须要提交审核, ...

  6. RH133读书笔记(7)-Lab 7 Advanced Filesystem Mangement

    Lab 7 Advanced Filesystem Mangement Goal: Develop skills and knowlege related to Software RAID, LVM, ...

  7. 兔子--Spring基金会

    设计模式的基本目的: 对象之间的解耦.使用容器来管理组件.减少不同组件之间的耦合 控制返回,搜索请求委托给容器 将积极考虑被动接受 版权声明:本文博主原创文章,博客,未经同意不得转载.

  8. 左右margin top问题百分比值

    不想说今天心情有多差! 9点多開始发现一个"bug",需求是依据屏幕高度动态调整某个div的位置.代码大概是这种. <div style="margin-top: ...

  9. Cookie和Session (转)

    Session和Cookie在网站开发中是用来保存用户与后端服务器的交互状态.它们有各自的缺点和优点.而且,他们的优点和应用场景是对立的.   Cookie 完整地描述:当一个用户通过HTTP访问一个 ...

  10. R0-R37它是Arm 寄存器,那是,CPU内部。和GPIO注册所有外设。换句话说,要是arm的cpu,它包含了其他芯片公司将有R0-R37,和GPIO寄存器只有一个特定的芯片。

    R0-R37它是Arm 寄存器.那是,CPU内部.和GPIO注册所有外设. 换句话说,要是arm的cpu,它包含了其他芯片公司将有R0-R37,和GPIO有. 版权声明:本文博主原创文章.博客,未经同 ...