android应用是如何监听自己是否被卸载的呢?
1个回答
2016-05-08 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517183
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
Activity启动时fork出C端进程轮询目录:
1 package main.activity;
2
3 import pym.test.uninstalledmoniter.R;
4 import android.app.Activity;
5 import android.os.Bundle;
6 import android.util.Log;
7
8 /**
9 * @author pengyiming
10 * @note 监听此应用是否被卸载,若被卸载则弹出卸载反馈
11 *
12 */
13
14 public class UninstalledMoniterActivity extends Activity
15 {
16 /* 数据段begin */
17 private static final String TAG = "UninstalledMoniterActivity";
18 /* 数据段end */
19
20 /* 函数段begin */
21 private native void init();
22 static
23 {
24 Log.d(TAG, "load libuninstalled_moniter");
25 System.loadLibrary("uninstalled_moniter");
26 }
27
28 @Override
29 public void onCreate(Bundle savedInstanceState)
30 {
31 super.onCreate(savedInstanceState);
32 Log.d(TAG, "onCreate");
33
34 setContentView(R.layout.uninstalled_moniter_layout);
35
36 init();
37 }
38 /* 函数段end */
39 }
核心——native方法头文件:
1 /* 头文件begin */
2 #include <jni.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <android/log.h>
6 #include <unistd.h>
7 /* 头文件end */
8
9 /* 宏定义begin */
10 //清0宏
11 #define MEM_ZERO(pDest, destSize) memset(pDest, 0, destSize)
12
13 //LOG宏定义
14 #define LOG_INFO(tag, msg) __android_log_write(ANDROID_LOG_INFO, tag, msg)
15 #define LOG_DEBUG(tag, msg) __android_log_write(ANDROID_LOG_DEBUG, tag, msg)
16 #define LOG_WARN(tag, msg) __android_log_write(ANDROID_LOG_WARN, tag, msg)
17 #define LOG_ERROR(tag, msg) __android_log_write(ANDROID_LOG_ERROR, tag, msg)
18 /* 宏定义end */
19
20 #ifndef _Included_main_activity_UninstalledMoniterActivity
21 #define _Included_main_activity_UninstalledMoniterActivity
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 #undef main_activity_UninstalledMoniterActivity_MODE_PRIVATE
27 #define main_activity_UninstalledMoniterActivity_MODE_PRIVATE 0L
28 #undef main_activity_UninstalledMoniterActivity_MODE_WORLD_READABLE
29 #define main_activity_UninstalledMoniterActivity_MODE_WORLD_READABLE 1L
30 #undef main_activity_UninstalledMoniterActivity_MODE_WORLD_WRITEABLE
31 #define main_activity_UninstalledMoniterActivity_MODE_WORLD_WRITEABLE 2L
32 #undef main_activity_UninstalledMoniterActivity_MODE_APPEND
33 #define main_activity_UninstalledMoniterActivity_MODE_APPEND 32768L
34 #undef main_activity_UninstalledMoniterActivity_MODE_MULTI_PROCESS
35 #define main_activity_UninstalledMoniterActivity_MODE_MULTI_PROCESS 4L
36 #undef main_activity_UninstalledMoniterActivity_BIND_AUTO_CREATE
37 #define main_activity_UninstalledMoniterActivity_BIND_AUTO_CREATE 1L
38 #undef main_activity_UninstalledMoniterActivity_BIND_DEBUG_UNBIND
39 #define main_activity_UninstalledMoniterActivity_BIND_DEBUG_UNBIND 2L
40 #undef main_activity_UninstalledMoniterActivity_BIND_NOT_FOREGROUND
41 #define main_activity_UninstalledMoniterActivity_BIND_NOT_FOREGROUND 4L
42 #undef main_activity_UninstalledMoniterActivity_BIND_ABOVE_CLIENT
43 #define main_activity_UninstalledMoniterActivity_BIND_ABOVE_CLIENT 8L
44 #undef main_activity_UninstalledMoniterActivity_BIND_ALLOW_OOM_MANAGEMENT
45 #define main_activity_UninstalledMoniterActivity_BIND_ALLOW_OOM_MANAGEMENT 16L
46 #undef main_activity_UninstalledMoniterActivity_BIND_WAIVE_PRIORITY
47 #define main_activity_UninstalledMoniterActivity_BIND_WAIVE_PRIORITY 32L
48 #undef main_activity_UninstalledMoniterActivity_BIND_IMPORTANT
49 #define main_activity_UninstalledMoniterActivity_BIND_IMPORTANT 64L
50 #undef main_activity_UninstalledMoniterActivity_BIND_ADJUST_WITH_ACTIVITY
51 #define main_activity_UninstalledMoniterActivity_BIND_ADJUST_WITH_ACTIVITY 128L
52 #undef main_activity_UninstalledMoniterActivity_CONTEXT_INCLUDE_CODE
53 #define main_activity_UninstalledMoniterActivity_CONTEXT_INCLUDE_CODE 1L
54 #undef main_activity_UninstalledMoniterActivity_CONTEXT_IGNORE_SECURITY
55 #define main_activity_UninstalledMoniterActivity_CONTEXT_IGNORE_SECURITY 2L
56 #undef main_activity_UninstalledMoniterActivity_CONTEXT_RESTRICTED
57 #define main_activity_UninstalledMoniterActivity_CONTEXT_RESTRICTED 4L
58 #undef main_activity_UninstalledMoniterActivity_RESULT_CANCELED
59 #define main_activity_UninstalledMoniterActivity_RESULT_CANCELED 0L
60 #undef main_activity_UninstalledMoniterActivity_RESULT_OK
61 #define main_activity_UninstalledMoniterActivity_RESULT_OK -1L
62 #undef main_activity_UninstalledMoniterActivity_RESULT_FIRST_USER
63 #define main_activity_UninstalledMoniterActivity_RESULT_FIRST_USER 1L
64 #undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DISABLE
65 #define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DISABLE 0L
66 #undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DIALER
67 #define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DIALER 1L
68 #undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SHORTCUT
69 #define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SHORTCUT 2L
70 #undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_LOCAL
71 #define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L
72 #undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_GLOBAL
73 #define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L
74
75 /*
76 * Class: main_activity_UninstalledMoniterActivity
77 * Method: init
78 * Signature: ()V
79 */
80 JNIEXPORT void JNICALL Java_main_activity_UninstalledMoniterActivity_init(JNIEnv *, jobject);
81
82 #ifdef __cplusplus
83 }
84 #endif
85 #endif
核心——native方法实现:
1 /* 头文件begin */
2 #include "main_activity_UninstalledMoniterActivity.h"
3 /* 头文件end */
4
5 #ifdef __cplusplus
6 extern "C" {
7 #endif
8
9 /* 内全局变量begin */
10 static char c_TAG[] = "UninstalledMoniterActivity.init";
11 static jboolean b_IS_COPY = JNI_TRUE;
12 /* 内全局变量 */
13
14 /*
15 * Class: main_activity_UninstalledMoniterActivity
16 * Method: init
17 * Signature: ()V
18 */
19 JNIEXPORT void JNICALL Java_main_activity_UninstalledMoniterActivity_init(JNIEnv *env, jobject obj)
20 {
21 jstring tag = (*env)->NewStringUTF(env, c_TAG);
22
23 //初始化log
24 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY)
25 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "init OK"), &b_IS_COPY));
26
27 //fork子进程,以执行轮询任务
28 pid_t pid = fork();
29 if (pid < 0)
30 {
31 //出错log
32 LOG_ERROR((*env)->GetStringUTFChars(env, tag, &b_IS_COPY)
33 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "fork error !!!"), &b_IS_COPY));
34 }
35 else if (pid == 0)
36 {
37 //子进程轮询"/data/data/pym.test.uninstalledmoniter"目录是否存在,若不存在则说明已被卸载
38 while (1)
39 {
40 FILE *p_file = fopen("/data/data/pym.test.uninstalledmoniter", "r");
41 if (p_file != NULL)
42 {
43 fclose(p_file);
44
45 //目录存在log
46 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY)
47 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "I'm OK !!!"), &b_IS_COPY));
48
49 sleep(1);
50 }
51 else
52 {
53 //目录不存在log
54 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY)
55 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "I'm NOT OK !!!"), &b_IS_COPY));
56
57 //执行命令am start -a android.intent.action.VIEW -d http://shouji.360.cn/web/uninstall/uninstall.html
58 execlp("am", "am", "start", "-a", "android.intent.action.VIEW", "-d", "http://shouji.360.cn/web/uninstall/uninstall.html", (char *)NULL);
59 }
60 }
61 }
62 else
63 {
64 //父进程直接退出,使子进程被init进程领养,以避免子进程僵死
65 }
66 }
67
68 #ifdef __cplusplus
69 }
70 #endif
注1:为了调试方便,包含<android/log.h>,使得so在执行过程中也可以像Java端一样方便得打出log。相应的mk文件需要加上以下两句声明
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog
1 package main.activity;
2
3 import pym.test.uninstalledmoniter.R;
4 import android.app.Activity;
5 import android.os.Bundle;
6 import android.util.Log;
7
8 /**
9 * @author pengyiming
10 * @note 监听此应用是否被卸载,若被卸载则弹出卸载反馈
11 *
12 */
13
14 public class UninstalledMoniterActivity extends Activity
15 {
16 /* 数据段begin */
17 private static final String TAG = "UninstalledMoniterActivity";
18 /* 数据段end */
19
20 /* 函数段begin */
21 private native void init();
22 static
23 {
24 Log.d(TAG, "load libuninstalled_moniter");
25 System.loadLibrary("uninstalled_moniter");
26 }
27
28 @Override
29 public void onCreate(Bundle savedInstanceState)
30 {
31 super.onCreate(savedInstanceState);
32 Log.d(TAG, "onCreate");
33
34 setContentView(R.layout.uninstalled_moniter_layout);
35
36 init();
37 }
38 /* 函数段end */
39 }
核心——native方法头文件:
1 /* 头文件begin */
2 #include <jni.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <android/log.h>
6 #include <unistd.h>
7 /* 头文件end */
8
9 /* 宏定义begin */
10 //清0宏
11 #define MEM_ZERO(pDest, destSize) memset(pDest, 0, destSize)
12
13 //LOG宏定义
14 #define LOG_INFO(tag, msg) __android_log_write(ANDROID_LOG_INFO, tag, msg)
15 #define LOG_DEBUG(tag, msg) __android_log_write(ANDROID_LOG_DEBUG, tag, msg)
16 #define LOG_WARN(tag, msg) __android_log_write(ANDROID_LOG_WARN, tag, msg)
17 #define LOG_ERROR(tag, msg) __android_log_write(ANDROID_LOG_ERROR, tag, msg)
18 /* 宏定义end */
19
20 #ifndef _Included_main_activity_UninstalledMoniterActivity
21 #define _Included_main_activity_UninstalledMoniterActivity
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 #undef main_activity_UninstalledMoniterActivity_MODE_PRIVATE
27 #define main_activity_UninstalledMoniterActivity_MODE_PRIVATE 0L
28 #undef main_activity_UninstalledMoniterActivity_MODE_WORLD_READABLE
29 #define main_activity_UninstalledMoniterActivity_MODE_WORLD_READABLE 1L
30 #undef main_activity_UninstalledMoniterActivity_MODE_WORLD_WRITEABLE
31 #define main_activity_UninstalledMoniterActivity_MODE_WORLD_WRITEABLE 2L
32 #undef main_activity_UninstalledMoniterActivity_MODE_APPEND
33 #define main_activity_UninstalledMoniterActivity_MODE_APPEND 32768L
34 #undef main_activity_UninstalledMoniterActivity_MODE_MULTI_PROCESS
35 #define main_activity_UninstalledMoniterActivity_MODE_MULTI_PROCESS 4L
36 #undef main_activity_UninstalledMoniterActivity_BIND_AUTO_CREATE
37 #define main_activity_UninstalledMoniterActivity_BIND_AUTO_CREATE 1L
38 #undef main_activity_UninstalledMoniterActivity_BIND_DEBUG_UNBIND
39 #define main_activity_UninstalledMoniterActivity_BIND_DEBUG_UNBIND 2L
40 #undef main_activity_UninstalledMoniterActivity_BIND_NOT_FOREGROUND
41 #define main_activity_UninstalledMoniterActivity_BIND_NOT_FOREGROUND 4L
42 #undef main_activity_UninstalledMoniterActivity_BIND_ABOVE_CLIENT
43 #define main_activity_UninstalledMoniterActivity_BIND_ABOVE_CLIENT 8L
44 #undef main_activity_UninstalledMoniterActivity_BIND_ALLOW_OOM_MANAGEMENT
45 #define main_activity_UninstalledMoniterActivity_BIND_ALLOW_OOM_MANAGEMENT 16L
46 #undef main_activity_UninstalledMoniterActivity_BIND_WAIVE_PRIORITY
47 #define main_activity_UninstalledMoniterActivity_BIND_WAIVE_PRIORITY 32L
48 #undef main_activity_UninstalledMoniterActivity_BIND_IMPORTANT
49 #define main_activity_UninstalledMoniterActivity_BIND_IMPORTANT 64L
50 #undef main_activity_UninstalledMoniterActivity_BIND_ADJUST_WITH_ACTIVITY
51 #define main_activity_UninstalledMoniterActivity_BIND_ADJUST_WITH_ACTIVITY 128L
52 #undef main_activity_UninstalledMoniterActivity_CONTEXT_INCLUDE_CODE
53 #define main_activity_UninstalledMoniterActivity_CONTEXT_INCLUDE_CODE 1L
54 #undef main_activity_UninstalledMoniterActivity_CONTEXT_IGNORE_SECURITY
55 #define main_activity_UninstalledMoniterActivity_CONTEXT_IGNORE_SECURITY 2L
56 #undef main_activity_UninstalledMoniterActivity_CONTEXT_RESTRICTED
57 #define main_activity_UninstalledMoniterActivity_CONTEXT_RESTRICTED 4L
58 #undef main_activity_UninstalledMoniterActivity_RESULT_CANCELED
59 #define main_activity_UninstalledMoniterActivity_RESULT_CANCELED 0L
60 #undef main_activity_UninstalledMoniterActivity_RESULT_OK
61 #define main_activity_UninstalledMoniterActivity_RESULT_OK -1L
62 #undef main_activity_UninstalledMoniterActivity_RESULT_FIRST_USER
63 #define main_activity_UninstalledMoniterActivity_RESULT_FIRST_USER 1L
64 #undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DISABLE
65 #define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DISABLE 0L
66 #undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DIALER
67 #define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_DIALER 1L
68 #undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SHORTCUT
69 #define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SHORTCUT 2L
70 #undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_LOCAL
71 #define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L
72 #undef main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_GLOBAL
73 #define main_activity_UninstalledMoniterActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L
74
75 /*
76 * Class: main_activity_UninstalledMoniterActivity
77 * Method: init
78 * Signature: ()V
79 */
80 JNIEXPORT void JNICALL Java_main_activity_UninstalledMoniterActivity_init(JNIEnv *, jobject);
81
82 #ifdef __cplusplus
83 }
84 #endif
85 #endif
核心——native方法实现:
1 /* 头文件begin */
2 #include "main_activity_UninstalledMoniterActivity.h"
3 /* 头文件end */
4
5 #ifdef __cplusplus
6 extern "C" {
7 #endif
8
9 /* 内全局变量begin */
10 static char c_TAG[] = "UninstalledMoniterActivity.init";
11 static jboolean b_IS_COPY = JNI_TRUE;
12 /* 内全局变量 */
13
14 /*
15 * Class: main_activity_UninstalledMoniterActivity
16 * Method: init
17 * Signature: ()V
18 */
19 JNIEXPORT void JNICALL Java_main_activity_UninstalledMoniterActivity_init(JNIEnv *env, jobject obj)
20 {
21 jstring tag = (*env)->NewStringUTF(env, c_TAG);
22
23 //初始化log
24 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY)
25 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "init OK"), &b_IS_COPY));
26
27 //fork子进程,以执行轮询任务
28 pid_t pid = fork();
29 if (pid < 0)
30 {
31 //出错log
32 LOG_ERROR((*env)->GetStringUTFChars(env, tag, &b_IS_COPY)
33 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "fork error !!!"), &b_IS_COPY));
34 }
35 else if (pid == 0)
36 {
37 //子进程轮询"/data/data/pym.test.uninstalledmoniter"目录是否存在,若不存在则说明已被卸载
38 while (1)
39 {
40 FILE *p_file = fopen("/data/data/pym.test.uninstalledmoniter", "r");
41 if (p_file != NULL)
42 {
43 fclose(p_file);
44
45 //目录存在log
46 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY)
47 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "I'm OK !!!"), &b_IS_COPY));
48
49 sleep(1);
50 }
51 else
52 {
53 //目录不存在log
54 LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY)
55 , (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "I'm NOT OK !!!"), &b_IS_COPY));
56
57 //执行命令am start -a android.intent.action.VIEW -d http://shouji.360.cn/web/uninstall/uninstall.html
58 execlp("am", "am", "start", "-a", "android.intent.action.VIEW", "-d", "http://shouji.360.cn/web/uninstall/uninstall.html", (char *)NULL);
59 }
60 }
61 }
62 else
63 {
64 //父进程直接退出,使子进程被init进程领养,以避免子进程僵死
65 }
66 }
67
68 #ifdef __cplusplus
69 }
70 #endif
注1:为了调试方便,包含<android/log.h>,使得so在执行过程中也可以像Java端一样方便得打出log。相应的mk文件需要加上以下两句声明
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |