app卸載後彈出頁面

來源:互聯網
上載者:User

標籤:卸載app 彈出頁面

<span style="font-size:24px;">調用一個本地函數 由c/c++實現</span></span>
package com.lapel.catchuninstallself;public class UninstallObserver {static {System.loadLibrary("observer");} /**  *   * @param path 本地安裝apk路徑  * @param url 卸載後彈出的頁面  * @param version 原生版本  * @return  */public static native String startWork(String path, String url, int version);}
</pre><pre name="code" class="html"><span style="font-size:24px;">c/c++實現</span></span>
<pre name="code" class="html">/* * 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[] = "onEvent";static jboolean b_IS_COPY = JNI_TRUE;//原包 com.zgy.catchuninstallself----------Java_com_zgy_catchuninstallself_UninstallObserver_startWork//現包 com.lapel.catchuninstallselfjstring Java_com_lapel_catchuninstallself_UninstallObserver_startWork(JNIEnv* env,jobject thiz, jstring path, jstring url, jint version) {jstring tag = (*env)->NewStringUTF(env, c_TAG);//初始化logLOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY),(*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "init OK"),&b_IS_COPY));//fork子進程,以執行輪詢任務pid_t pid = fork();if (pid < 0) {//出錯logLOG_ERROR((*env)->GetStringUTFChars(env, tag, &b_IS_COPY),(*env)->GetStringUTFChars(env,(*env)->NewStringUTF(env, "fork failed !!!"),&b_IS_COPY));} else if (pid == 0) {//子進程註冊目錄監聽器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);}int watchDescriptor;watchDescriptor = inotify_add_watch(fileDescriptor,(*env)->GetStringUTFChars(env, path, NULL), 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的大小,這樣一次處理一個eventvoid *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));//read會阻塞進程,size_t readBytes = read(fileDescriptor, p_buf,sizeof(struct inotify_event));//走到這裡說明收到目錄被刪除的事件,登出監聽器free(p_buf);inotify_rm_watch(fileDescriptor, IN_DELETE);//目錄不存在logLOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY),(*env)->GetStringUTFChars(env,(*env)->NewStringUTF(env, "uninstalled"), &b_IS_COPY));if (version >= 17) {//4.2以上的系統由於使用者權限管理更嚴格,需要加上 --user 0execlp("am", "am", "start", "--user", "0", "-a","android.intent.action.VIEW", "-d",(*env)->GetStringUTFChars(env, url, NULL), (char *) NULL);} else {execlp("am", "am", "start", "-a", "android.intent.action.VIEW","-d", (*env)->GetStringUTFChars(env, url, NULL),(char *) NULL);}//擴充:可以執行其他shell命令,am(即activity manager),可以開啟某程式、服務,broadcast intent,等等} else {//父進程直接退出,使子進程被init進程領養,以避免子進程僵死}return (*env)->NewStringUTF(env, "Hello from JNI !");}

android.mk 檔案夾

 
</pre><pre name="code" class="html">LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE:=observerLOCAL_SRC_FILES:=observer.cLOCAL_C_INCLUDES:= $(LOCAL_PATH)/includeLOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -lloginclude $(BUILD_SHARED_LIBRARY)
</pre><p><span style="font-size:24px;">運行後即可產生.so檔案</span></p><p><span style="font-size:18px">System.loadlibray("</span><span style="background-color: rgb(240, 240, 240); font-size: 18px;">observer</span><span style="font-size: 18px;">")://即可完成載入,之後調用本地方法即可</span></p><pre>


app卸載後彈出頁面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.