Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 應用卸載後彈出用戶反饋界面的 NDK 實作原理

編輯:關於 Android 程式設計

在 Android 開發中,我們有時會有這樣的需求:當用戶卸載了我們的 App 時,如果能收集到用戶的反饋資訊,那將非常有價值。本文就手把手帶大家實現這個功能。先說原理:App 安裝時會在 /data/data/包名 下生成一個專屬資料夾,一旦應用被卸載,這個資料夾也會隨之被移除。因此,我們可以用底層 C 程式碼不斷監聽這個資料夾是否存在,以此判斷 App 是否被卸載。

一、聲明 native 方法

首先建立一個 NativeClass,在其中聲明一個本地方法:

 package com.example.uninstallprompt;
public class NativeClass {
    public native String init();
}

二、生成 .h 頭文件

在生成頭檔之前,需要先編譯出 .class 檔。

接著用 javah 產生 .h 頭檔。

注意這裡的格式為:javah -classpath src路徑 -jni 包名.類名稱。執行後會在 src 路徑下生成 .h 頭檔。

內容如下:

 /* DO NOT EDIT THIS FILE - it is machine generated */
#include 
/* Header for class com_example_uninstallprompt_NativeClass */
#ifndef _Included_com_example_uninstallprompt_NativeClass
#define _Included_com_example_uninstallprompt_NativeClass
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class: com_example_uninstallprompt_NativeClass
 * Method: init
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_example_uninstallprompt_NativeClass_init (JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif

三、編寫 C 程式碼

在 Android 工程中新建一個 jni 目錄,並在其中建立 uninstall-jni.c 檔,把剛才產生的 .h 檔內容拷貝進來,同時把生成的 com_example_uninstallprompt_NativeClass.h 也拷到該文件夾下。uninstall-jni.c 內容如下:

 #include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "com_example_uninstallprompt_NativeClass.h"
#define MEM_ZERO(pDest, destSize) memset(pDest, 0, destSize)
#define LOG_TAG "onEvent"
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##args)
JNIEXPORT jstring JNICALL Java_com_example_uninstallprompt_NativeClass_init(JNIEnv* env, jobject thiz) {
    LOGD("init start...");
    // fork 子行程以執行輪詢任務
    pid_t pid = fork();
    if (pid < 0) {
        LOGD("fork failed...");
    } else if (pid == 0) {
        // 子行程註冊 /data/data/com.example.uninstallprompt 目錄監聽器
        int fileDescriptor = inotify_init();
        if (fileDescriptor < 0) { LOGD("inotify_init failed..."); exit(1); }
        int watchDescriptor;
        watchDescriptor = inotify_add_watch(fileDescriptor, "/data/data/com.example.uninstallprompt", IN_DELETE);
        LOGD("watchDescriptor=%d", watchDescriptor);
        if (watchDescriptor < 0) { LOGD("inotify_add_watch failed..."); exit(1); }
        // 分配緩存以讀取 event,一次處理一個 event
        void *p_buf = malloc(sizeof(struct inotify_event));
        if (p_buf == NULL) { LOGD("malloc failed..."); exit(1); }
        LOGD("start observer...");
        size_t readBytes = read(fileDescriptor, p_buf, sizeof(struct inotify_event));
        // read 會阻塞行程,走到這裡說明收到目錄被刪除的事件
        free(p_buf);
        inotify_rm_watch(fileDescriptor, IN_DELETE);
        LOGD("uninstall");
        // 執行 am start 命令打開反饋頁面;4.2 以上系統需加 --user 0
        execlp("am", "am", "start", "--user", "0", "-a", "android.intent.action.VIEW",
               "-d", "https://www.baidu.com", (char *)NULL);
    } else {
        // 父行程直接退出,使子行程被 init 行程領養,避免僵死
    }
    return (*env)->NewStringUTF(env, "Hello from JNI !");
} 

這裡我在該應用被卸載時,跳轉到百度的主介面。

四、編寫 Android.mk 檔

在剛才新建的 jni 目錄下建立 Android.mk 檔,內容如下:

 LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := uninstall-jni
LOCAL_SRC_FILES := uninstall-jni.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY) 

說明一下:LOCAL_MODULE := uninstall-jni 表示編譯出來的模組名稱,這個名稱可隨意命名,但在 Java 檔中 loadLibrary 時必須一致,一般與要編譯的 .c 檔名保持一致。LOCAL_SRC_FILES := uninstall-jni.c 則表示需要編譯的 .c 檔。

五、編譯 C 程式碼

可以看到,這裡先進入工程根目錄,然後執行 ndk-build 編譯 Android.mk 中聲明的 C 程式碼。編譯完成後,工程中會多出如下文件夾與檔:

如果某些步驟不太清楚,建議先系統地學習 Android NDK 開發的基礎知識,再回頭對照本文的每個環節。

這裡寫圖片描述

好了,今天就到這裡,希望大家能夠喜歡。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved