本文,提供“Android应用被卸载后,自动使用 浏览器打开指定连接”的方法。
原理:在安卓程序中某处,基于JNI调用C代码开启一个子进程监控应用在系统中的文件目录,一旦应用被卸载,该目录将会被系统删除,此时触发子进程执行相关代码(本例调用浏览器执行打开一个连接)
1、在安卓项目下创建jni目录
2、在jni目录下创建文件observer.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 <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <android/log.h>
#include <unistd.h>
#include <sys/inotify.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)
/* 内全局变量begin */
static char c_TAG[] = "OnAppUninstall";
static jboolean b_IS_COPY = JNI_TRUE;
/* native方法定义 */
JNIEXPORT jstring JNICALL Java_com_zgy_catchuninstallself_UninstallObserver_startWork(
JNIEnv* env, jobject thiz, jstring dirStr, jstring data_packagedir, jstring activity,
jstring action, jstring action_data, jstring userSerial) {
jstring tag = (*env)->NewStringUTF(env, c_TAG);
LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY), (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "start------------------"), &b_IS_COPY)); //初始化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(); char showpid[10]; sprintf(showpid, "%d", pid); LOG_INFO((*env)->GetStringUTFChars(env, tag, &b_IS_COPY), (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, showpid), &b_IS_COPY)); 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) { LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY), (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "childen process"), &b_IS_COPY)); 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); } LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY), (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "will creat a lockfile"), &b_IS_COPY)); LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY), (*env)->GetStringUTFChars(env, dirStr, &b_IS_COPY)); LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY), (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "creat a new process"), &b_IS_COPY)); //子进程注册"/data/data/{package}"目录监听器 int watchDescriptor; watchDescriptor = inotify_add_watch(fileDescriptor, (*env)->GetStringUTFChars(env, dirStr, &b_IS_COPY), 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)); size_t readBytes = read(fileDescriptor, p_buf, sizeof(struct inotify_event)); //等待5秒 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY), (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "sleep 2 seconds"), &b_IS_COPY)); sleep(2); //如果是覆盖安装跳过 FILE *p_appDir = fopen(data_packagedir, "r"); if (p_appDir != NULL) { LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY), (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "reinstall"), &b_IS_COPY)); exit(1); } //read会阻塞进程,走到这里说明收到目录被删除的事件,注销监听器 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)); //执行命令am start -a android.intent.action.VIEW -d http://ww.baidu.com // execlp("am", "am", "start", "-a", "android.intent.action.VIEW", "-d", "http://ww.baidu.com", (char *)NULL); //4.2以上的系统由于用户权限管理更严格,需要加上 --user 0 if (userSerial == NULL) { // 执行命令am start -a android.intent.action.VIEW -d $(url) execlp("am", "am", "start", "-a", "android.intent.action.VIEW", "-d", (*env)->GetStringUTFChars(env, action_data, &b_IS_COPY), (char *) NULL); } else { if (activity == NULL || (*env)->GetStringUTFLength(env, activity) < 1) { execlp("am", "am", "start", "--user", (*env)->GetStringUTFChars(env, userSerial, &b_IS_COPY), "-a", (*env)->GetStringUTFChars(env, action, &b_IS_COPY), "-d", (*env)->GetStringUTFChars(env, action_data, &b_IS_COPY), (char *) NULL); } else { if (action == NULL || (*env)->GetStringUTFLength(env, action) < 1) { if (action_data == NULL || (*env)->GetStringUTFLength(env, action_data) < 1) { execlp("am", "am", "start", "--user", (*env)->GetStringUTFChars(env, userSerial, &b_IS_COPY), "-n", (*env)->GetStringUTFChars(env, activity, &b_IS_COPY), (char *) NULL); } else { execlp("am", "am", "start", "--user", (*env)->GetStringUTFChars(env, userSerial, &b_IS_COPY), "-n", (*env)->GetStringUTFChars(env, activity, &b_IS_COPY), "-d", (*env)->GetStringUTFChars(env, action_data, &b_IS_COPY), (char *) NULL); } } else { if (action_data == NULL || (*env)->GetStringUTFLength(env, action_data) < 1) { execlp("am", "am", "start", "--user", (*env)->GetStringUTFChars(env, userSerial, &b_IS_COPY), "-n", (*env)->GetStringUTFChars(env, activity, &b_IS_COPY), "-a", (*env)->GetStringUTFChars(env, action, &b_IS_COPY), (char *) NULL); } else { execlp("am", "am", "start", "--user", (*env)->GetStringUTFChars(env, userSerial, &b_IS_COPY), "-n", (*env)->GetStringUTFChars(env, activity, &b_IS_COPY), "-a", (*env)->GetStringUTFChars(env, action, &b_IS_COPY), "-d", (*env)->GetStringUTFChars(env, action_data, &b_IS_COPY), (char *) NULL); } } } } //扩展:可以执行其他shell命令,am(即activity manager),可以打开某程序、服务,broadcast intent,等等 } else { //父进程直接退出,使子进程被init进程领养,以避免子进程僵死 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY), (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "fork pid > 0"), &b_IS_COPY)); } LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY), (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "end------------------"), &b_IS_COPY)); return (*env)->NewStringUTF(env, showpid);
}
3、在jni目录下创建文件Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:=observer
LOCAL_SRC_FILES:=observer.c
LOCAL_C_INCLUDES:= $(LOCAL_PATH)/include
LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)
4、在创建natvie方法,并在Android中调用JNI方法
package com.zgy.catchuninstallself;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.os.Build;
import android.util.Log;
/** * 应用程序卸载监控,入口调用方法:openUrlWhenUninstall(……) * * @author SHANHY([email protected]) * @date 2015年12月15日 */
public class UninstallObserver {
// path:data/data/[packageNmae]/lib
// url:跳转的页面,需要http://或https://开头
/** * Native 方法 * * @param dirStr 要监听的目录,传值:/data/data/{packageNmae}/lib * @param activity Activity对象 * @param action android.intent.action.VIEW * @param actionData 要在浏览器中打开的URL * @param userSerial * @return * @author SHANHY * @date 2015年12月15日 */
private static native String startWork(String dirStr,String dataPackageDir, String activity, String action, String actionData, String userSerial);
static {
System.loadLibrary("observer");
}
private static SharedPreferences sharedPreferences;
private static SharedPreferences.Editor editor;
/** * 在需要开启子进程监听的地方调用该方法 * * @param context * @param openUrl * @author SHANHY * @date 2015年12月15日 */
public static void openUrlWhenUninstall(Context context, String openUrl) {
sharedPreferences = context.getSharedPreferences("process", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
String pid = sharedPreferences.getString("pid", "0");
if (checkChildProcess(context, pid)) {
Log.i("OnAppUninstall", "已经开启过守护进程");
return;
}
Log.i("OnAppUninstall", "执行开启过守护进程");
String dataPackageDir = context.getApplicationInfo().dataDir;
String dirStr = context.getApplicationInfo().dataDir + "/lib";// 监听lib目录是因为:lib目录在进行应用清理的时候不会被清理
String activity = null;
if (checkInstall(context, "com.android.browser")) {
activity = "com.android.browser/com.android.browser.BrowserActivity";
}
String action = "android.intent.action.VIEW";
String gpid;
if (Build.VERSION.SDK_INT < 17) {
gpid = startWork(dirStr, dataPackageDir, activity, action, openUrl, null);
} else {//4.2以上的系统由于用户权限管理更严格,调用API有所区别
gpid = startWork(dirStr, dataPackageDir, activity, action, openUrl, getUserSerial(context));
}
if (gpid != null && !gpid.equals("") && !gpid.equals("0")) {
Log.i("OnAppUninstall", "获得守护进程pid-->" + gpid);
editor.putString("pid", gpid);
editor.commit();
}
}
/** * 检查安装 * * @param context * @param packageName * @return * @author SHANHY * @date 2015年12月15日 */
private static boolean checkInstall(Context context, String packageName) {
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
if (packageInfo == null) {
return false;
} else {
return true;
}
} catch (Exception e) {
return false;
}
}
/** * 获取 UserSerial * * @param ctx * @return * @author SHANHY * @date 2015年12月15日 */
private static String getUserSerial(Context ctx) {
Object userManager = ctx.getSystemService("user");
if (userManager == null) {
return null;
}
try {
Method myUserHandleMethod = android.os.Process.class.getMethod("myUserHandle", (Class<?>[]) null);
Object myUserHandle = myUserHandleMethod.invoke(android.os.Process.class, (Object[]) null);
Method getSerialNumberForUser = userManager.getClass().getMethod("getSerialNumberForUser", myUserHandle.getClass());
long userSerial = (Long) getSerialNumberForUser.invoke(userManager, myUserHandle);
return String.valueOf(userSerial);
} catch (NoSuchMethodException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
return null;
}
/** * 检查子进程 * * @param context * @param mpid * @return * @author SHANHY * @date 2015年12月15日 */
private static boolean checkChildProcess(Context context, String mpid) {
boolean resflag = false;
int mypid = android.os.Process.myPid();
Log.i("OnAppUninstall", "mypid-->" + mypid);
BufferedReader in = null;
long starttime = System.currentTimeMillis();
List<Map<String, String>> listdata = new ArrayList<Map<String, String>>();
try {
Process p = Runtime.getRuntime().exec("ps");
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
String[] temp;
boolean flag = false;
// int length = 0;
while ((line = in.readLine()) != null) {
// Log.i("OnAppUninstall", "ps-->" + line);
if (!flag) {
flag = true;
continue;
}
line = line.replaceAll(" +", " ");
temp = line.split(" ");
// System.out.println(context.getPackageName());
// System.out.println(temp[8]);
if (temp[8].trim().equals(context.getPackageName().trim()) && (temp[2].trim().equals(mypid + "") || temp[2].trim().equals("1"))) {
Log.i("OnAppUninstall", "get it");
Map<String, String> map = new HashMap<String, String>();
map.put("pid", temp[1]);
map.put("pname", temp[8]);
listdata.add(map);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
in = null;
}
}
long endtime = System.currentTimeMillis();
long spendtime = endtime - starttime;
for (int i = 0; i < listdata.size(); i++) {
if (listdata.get(i).get("pid").equals(mpid)) {
resflag = true;
} else {
Log.w("OnAppUninstall", "捕获到多余守护进程-->" + listdata.get(i).get("pid") + " " + listdata.get(i).get("pname"));
try {
android.os.Process.killProcess(Integer.valueOf(listdata.get(i).get("pid").trim()));
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
Log.i("OnAppUninstall", listdata.get(i).get("pid") + " " + listdata.get(i).get("pname"));
}
Log.i("OnAppUninstall", "spendtime-->" + spendtime);
return resflag;
}
}
String url = "http://www.baidu.com";
UninstallObserver.openUrlWhenUninstall(this, url);
注:本文已经更新,并解决了之前“fork多个子进程”的问题。