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

首先建立一個 NativeClass,在其中聲明一個本地方法:
package com.example.uninstallprompt;
public class NativeClass {
public native String init();
}
在生成頭檔之前,需要先編譯出 .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
在 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 !");
}
這裡我在該應用被卸載時,跳轉到百度的主介面。
在剛才新建的 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 檔。

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

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

好了,今天就到這裡,希望大家能夠喜歡。
Android EasyBarrage 輕量級彈幕庫的使用方式
若不想從零實作彈幕,又想讓影片或直播頁有輕量互動效果,可以考慮使用現成的彈幕庫。EasyBarrage 就是這類方案之一,它提供基本的彈幕展示、隨機顏色、邊框與動態新增能
Android 用 setDataAndType 打開各種檔案:MIME 對照表與 Intent 範例大全
在 Android 中要用系統現有的應用打開某個檔案,關鍵是「正確的 MIME 類型」。只要透過 Intent.ACTION_VIEW 搭配 setDataAndType
Android 記憶體洩漏的常見成因總結
Java 是一種具備垃圾回收機制的語言,它的優點在於開發者無需特別管理記憶體配置,既降低了應用因局部故障(segmentation fault)而崩潰的風險,也避免了未釋
Android 使用 PullToRefresh 下拉刷新與上拉加載
PullToRefresh 是一套成熟的下拉刷新元件,支援 ListView、GridView、ExpandableListView 與 WebView 等常見可捲動 V