最近研究了一下如何监听自身应用卸载,最后,做了一个sample。
sample 下载地址:http://download.csdn.net/detail/keanbin/6991979
主要代码:
1. 监听本应用卸载,其实是监听应用在内部存储器中的文件夹是否被删除。
ListenUninstallProcess.c
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <string.h>
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/inotify.h>
#include "AndroidLog.h"
#include "CurlHttpOperate.h"
/* 宏定义begin */
//清0宏
#define MEM_ZERO(pDest, destSize) memset(pDest, 0, destSize)
//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)
//#define FEEDBACK_URL "http://www.baidu.com"
//#define LISTEN_FILE_PATH "/data/data/com.keanbin.app"
/* 内全局变量begin */
static jboolean b_IS_COPY = JNI_TRUE;
/*
* 启动进程,监听指定文件,如果文件被删除,就启动网页。
*
* listenFilePathJStr: 监听的文件路径
* urlJStr: 网址
*/
void Java_com_keanbin_testlistenselfuninstall_ListenUninstallProcess_startBrowser(
JNIEnv* env, jobject thiz, jstring listenFilePathJStr, jstring urlJStr,
jint androidSdkVersion) {
const char *listenFilePath;
const char *url;
//初始化log
LOGI("jni startBrowser()");
listenFilePath = (*env)->GetStringUTFChars(env, listenFilePathJStr, NULL);
if (listenFilePath == NULL) {
return;
}
url = (*env)->GetStringUTFChars(env, urlJStr, NULL);
if (url == NULL) {
return;
}
LOGI("jni startBrowser() listenFilePath = %s , url = %s",
listenFilePath, url);
//fork子进程,以执行轮询任务
pid_t pid = fork();
if (pid < 0) {
//出错log