#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <android/log.h>
#include <unistd.h>
#include <sys/inotify.h>
#include "com_example_uninstalldemos_NativeClass.h"
/* 宏定义begin */
//清0宏
#define MEM_ZERO(pDest, destSize) memset(pDest, 0, destSize)
#define LOG_TAG "onEvent"
//LOG宏定义
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##args)
JNIEXPORT jstring JNICALL Java_com_example_uninstalldemos_NativeClass_init(JNIEnv* env, jobject thiz) {
//初始化log
LOGD("init start...");
//fork子进程,以执行轮询任务
pid_t pid = fork();
if (pid < 0) {
//出错log
LOGD("fork failed...");
} else if (pid == 0) {
//子进程注册"/data/data/pym.test.uninstalledobserver"目录监听器
int fileDescriptor = inotify_init();
if (fileDescriptor < 0) {
LOGD("inotify_init failed...");
exit(1);
}
int watchDescriptor;
watchDescriptor = inotify_add_watch(fileDescriptor,"/data/data/com.example.uninstalldemos", IN_DELETE);
LOGD("watchDescriptor=%d",watchDescriptor);
if (watchDescriptor < 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...");
size_t readBytes = read(fileDescriptor, p_buf,sizeof(struct inotify_event));
//read会阻塞进程,走到这里说明收到目录被删除的事件,注销监听器
free(p_buf);
inotify_rm_watch(fileDescriptor, IN_DELETE);
//目录不存在log
LOGD("uninstall");
//执行命令am start -a android.intent.action.VIEW -d http://shouji.360.cn/web/uninstall/uninstall.html
execlp(
"am", "am", "start", "-a", "android.intent.action.VIEW", "-d",
"http://shouji.360.cn/web/uninstall/uninstall.html", (char *)NULL);
//4.2以上的系统由于用户权限管理更严格,需要加上 --user 0
//execlp("am", "am", "start", "--user", "0", "-a",
//"android.intent.action.VIEW", "-d", "https://www.google.com",(char *) NULL);
} else {
//父进程直接退出,使子进程被init进程领养,以避免子进程僵死
}
return (*env)->NewStringUTF(env, "Hello from JNI !");
}
package com.example.uninstalldemos;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, SDCardListenSer.class);
startService(intent);
NativeClass nativeObj = new NativeClass();
nativeObj.init();
}
static {
Log.d("onEvent", "load jni lib");
System.loadLibrary("hello-jni");
}
}
package com.example.uninstalldemos;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.FileObserver;
import android.os.IBinder;
import android.util.Log;
import java.io.File;
import java.io.IOException;
public class SDCardListenSer extends Service {
SDCardListener[] listenners;
@SuppressLint("SdCardPath")
@Override
public void onCreate() {
SDCardListener[] listenners = {
new SDCardListener("/data/data/com.example.uninstalldemos", this),
new SDCardListener(Environment.getExternalStorageDirectory() + File.separator + "1.txt", this) };
this.listenners = listenners;
Log.i("onEvent", "=========onCreate============");
for (SDCardListener listener : listenners) {
listener.startWatching();
}
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "1.txt");
Log.i("onEvent", "dddddddddddddddddddddd nCreate============");
if (file.exists())
file.delete();
/*try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}*/
}
@Override
public void onDestroy() {
for (SDCardListener listener : listenners) {
listener.stopWatching();
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
class SDCardListener extends FileObserver {
private String mPath;
private final Context mContext;
public SDCardListener(String parentpath, Context ctx) {
super(parentpath);
this.mPath = parentpath;
this.mContext = ctx;
}
@Override
public void onEvent(int event, String path) {
int action = event & FileObserver.ALL_EVENTS;
switch (action) {
case FileObserver.DELETE:
Log.i("onEvent", "delete path: " + mPath + File.separator + path);
//openBrowser();
break;
case FileObserver.MODIFY:
Log.i("onEvent", "更改目录" + mPath + File.separator + path);
break;
case FileObserver.CREATE:
Log.i("onEvent", "创建文件" + mPath + File.separator + path);
break;
default:
break;
}
}
protected void openBrowser() {
Uri uri = Uri.parse("http://aoi.androidesk.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
mContext.startActivity(intent);
}
public void exeShell(String cmd) {
try {
Runtime.getRuntime().exec(cmd);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
adb logcat -s onEvent[img]http://files.jb51.net/file_images/article/201604/2016428172750821.png?201632817280[/img]
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有