Last active
March 30, 2021 08:58
-
-
Save spiritedRunning/cacf53412cb1c537ff2aed16093c254f to your computer and use it in GitHub Desktop.
进程保护 监听文件是否存在
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JNIClass { | |
static { | |
System.loadLibrary("loopjni"); | |
} | |
public static native void createProcess(String pid); | |
public void restartActivity() { | |
try { | |
// Runtime.getRuntime().exec("am start --user 0 -n com.example/com.droidwolf.example.WatchDogActivity"); | |
Runtime.getRuntime().exec("am start -n com.example.free.loopmonitorapp/com.example.free.loopmonitorapp.MainActivity"); | |
} catch (IOException e) { | |
Log.e("JNIClass", "restartActivity error: " + e); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define LOGTAG "loopjni" | |
#define LOG_D(tag, msg) __android_log_write(ANDROID_LOG_DEBUG, tag, msg) | |
#define LOG_PRINT_D(tag, msg, ...) __android_log_print(ANDROID_LOG_DEBUG, tag, msg, __VA_ARGS__) | |
int watch(char *pidnum) { | |
char proc_path[100] = ""; | |
DIR *proc_dir; | |
LOG_PRINT_D(LOGTAG, "watch start, pidnum: %s", pidnum); | |
// 初始化进程目录 | |
strcpy(proc_path, "/proc/"); | |
strcat(proc_path, pidnum); | |
strcat(proc_path, "/"); | |
LOG_PRINT_D(LOGTAG, "proccess path is: %s", proc_path); | |
// 尝试访问它以确定是否存在 | |
proc_dir = opendir(proc_path); | |
if (proc_dir == NULL) { | |
LOG_PRINT_D(LOGTAG, "process: %s not exist! start it!", pidnum); | |
LOG_D(LOGTAG, "---------------------"); | |
return 1; | |
} | |
else { | |
LOG_PRINT_D(LOGTAG, "process: %s exist!", pidnum); | |
LOG_D(LOGTAG, "....................."); | |
return 0; | |
} | |
} | |
void restart_target(JNIEnv *env) { | |
// char cmd_restart[128] = "am startservice --user 0 -a com.a.b.service.YourService"; | |
// popen(cmd_restart, "r"); | |
// char cmd_restart[128] = "am start -n "com.example.free.loopmonitorapp/com.example.free.loopmonitorapp.MainActivity | |
// -a android.intent.action.MAIN -c android.intent.category.LAUNCHER"; | |
jclass clazz = (*env)->FindClass(env, "com/example/free/loopmonitorapp/JNIClass"); | |
jobject obj = (*env)->AllocObject(env, clazz); | |
(*env)->CallVoidMethod(env, obj, (*env)->GetMethodID(env, clazz, "restartActivity", "()V")); | |
(*env)->DeleteLocalRef(env, obj); | |
LOG_D(LOGTAG, "restart process task...."); | |
} | |
JNIEXPORT void JNICALL | |
Java_com_example_free_loopmonitorapp_JNIClass_createProcess(JNIEnv *env, jclass clazz, | |
jstring ppid) { | |
// pid_t ppid = getpid(); | |
pid_t pid = fork(); | |
if (pid < 0) { | |
LOG_D(LOGTAG, "create--fork failed!"); | |
} else if (pid == 0) { | |
LOG_D(LOGTAG, "create--runOnSubprocess start..."); | |
runOnSubprocess(env, clazz, ppid); | |
LOG_D(LOGTAG, "create--runOnSubprocess finished!"); | |
exit(1); | |
} else { | |
LOG_D(LOGTAG, "create--run on parent process!"); | |
} | |
} | |
void runOnSubprocess(JNIEnv *env, jclass clazz, jstring pid) { | |
LOG_D(LOGTAG, "START LOOP MONITORING"); | |
const char *nativePid = (*env)->GetStringUTFChars(env, pid, 0); | |
while (1) { | |
if (watch(nativePid)) { | |
restart_target(env); | |
break; | |
} | |
sleep(3); | |
} | |
(*env)->ReleaseStringUTFChars(env, pid, nativePid); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
JNIClass.createProcess(String.valueOf(android.os.Process.myPid())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment