檢索android.os.Build ....系統屬性我有一個純粹的原生Android NDK應用程序,並且需要檢索的值,如: android.os.Build.MODEL通過純粹的原生Android應用
不幸我找不到如何解決這個問題的好例子?
檢索android.os.Build ....系統屬性我有一個純粹的原生Android NDK應用程序,並且需要檢索的值,如: android.os.Build.MODEL通過純粹的原生Android應用
不幸我找不到如何解決這個問題的好例子?
NDK並不是要取代基於Java的API,而是要補充它。要獲得Build,必須在C/C++中找到它的私有實現,或者通過JNI提供來自Java的信息。
僞代碼:
android_main(struct *android_app){
JNIEnv *env = android_app->activity->env;
jclass build_class = FindClass(env, "android.os.Build");
jfieldID brand_id = GetStaticFieldID(env, build_class, "BRAND", "Ljava/lang/String;");
jstring brand_obj = (jstring)GetStaticObjectField(env, brand_id);
}
我不認爲你可以遺憾的是做到這一點。您可以始終在VM中啓動,並在檢索要訪問的值後通過JNI跳轉到本地。
這些值很容易通過<sys/system_properties.h>
中定義的接口在本地代碼中獲得,該接口自第一次NDK發佈以來一直存在。您只需要知道Java端使用的字符串標識符。幸運的是,使用開源操作系統,我們可以輕鬆找到這些操作系統。這是檢索型號名稱的一個工作示例。
#include <sys/system_properties.h>
//
// Public codes are defined in http://developer.android.com/reference/java/lang/System.html#getProperty(java.lang.String).
// Codes below are defined in https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/os/Build.java.
// Items with * are intended for display to the end user.
//
#define ANDROID_OS_BUILD_VERSION_RELEASE "ro.build.version.release" // * The user-visible version string. E.g., "1.0" or "3.4b5".
#define ANDROID_OS_BUILD_VERSION_INCREMENTAL "ro.build.version.incremental" // The internal value used by the underlying source control to represent this build.
#define ANDROID_OS_BUILD_VERSION_CODENAME "ro.build.version.codename" // The current development codename, or the string "REL" if this is a release build.
#define ANDROID_OS_BUILD_VERSION_SDK "ro.build.version.sdk" // The user-visible SDK version of the framework.
#define ANDROID_OS_BUILD_MODEL "ro.product.model" // * The end-user-visible name for the end product..
#define ANDROID_OS_BUILD_MANUFACTURER "ro.product.manufacturer" // The manufacturer of the product/hardware.
#define ANDROID_OS_BUILD_BOARD "ro.product.board" // The name of the underlying board, like "goldfish".
#define ANDROID_OS_BUILD_BRAND "ro.product.brand" // The brand (e.g., carrier) the software is customized for, if any.
#define ANDROID_OS_BUILD_DEVICE "ro.product.device" // The name of the industrial design.
#define ANDROID_OS_BUILD_PRODUCT "ro.product.name" // The name of the overall product.
#define ANDROID_OS_BUILD_HARDWARE "ro.hardware" // The name of the hardware (from the kernel command line or /proc).
#define ANDROID_OS_BUILD_CPU_ABI "ro.product.cpu.abi" // The name of the instruction set (CPU type + ABI convention) of native code.
#define ANDROID_OS_BUILD_CPU_ABI2 "ro.product.cpu.abi2" // The name of the second instruction set (CPU type + ABI convention) of native code.
#define ANDROID_OS_BUILD_DISPLAY "ro.build.display.id" // * A build ID string meant for displaying to the user.
#define ANDROID_OS_BUILD_HOST "ro.build.host"
#define ANDROID_OS_BUILD_USER "ro.build.user"
#define ANDROID_OS_BUILD_ID "ro.build.id" // Either a changelist number, or a label like "M4-rc20".
#define ANDROID_OS_BUILD_TYPE "ro.build.type" // The type of build, like "user" or "eng".
#define ANDROID_OS_BUILD_TAGS "ro.build.tags" // Comma-separated tags describing the build, like "unsigned,debug".
#define ANDROID_OS_BUILD_FINGERPRINT "ro.build.fingerprint" // A string that uniquely identifies this build. 'BRAND/PRODUCT/DEVICE:RELEASE/ID/VERSION.INCREMENTAL:TYPE/TAGS'.
char model_id[PROP_VALUE_MAX]; // PROP_VALUE_MAX from <sys/system_properties.h>.
int len;
len = __system_property_get(ANDROID_OS_BUILD_MODEL, model_id); // On return, len will equal (int)strlen(model_id).
謝謝。這正是缺少的信息,並且在公開的android ndk文檔中的任何地方都不明顯或可見。如果您對方法簽名和合約感興趣,可以通過以下鏈接找到源代碼:https://android.googlesource.com/platform/bionic/+/android-1.6_r1/libc/include/sys/system_properties.h –
謝謝你。我無法找到它的C/C++實現,但是我可以嘗試通過JNI從Java中檢索這個信息。我看到這樣的例子,'JNIEnv *'是一個函數參數,後來用於調用諸如'GetMethodID'和'CallVoidMethod'之類的方法,但是我不確定我可以從哪裏或如何調用我的本地方法?我將從哪裏得到這個JNIEnv *參數? –
(即我的純粹原生應用程序有'android_main'作爲我的純粹原生應用程序的入口點,因此我不知道如何調用一個函數,將JNIEnv *作爲參數) –
啊,這將是一個問題,您從Build尋找什麼樣的信息? –