Java 检索android.os.Build。。。。通过纯本地Android应用程序的系统属性
我有一个纯本地Android NDK应用程序,需要检索以下值: android.os.Build.MODELJava 检索android.os.Build。。。。通过纯本地Android应用程序的系统属性,java,android,java-native-interface,android-ndk,Java,Android,Java Native Interface,Android Ndk,我有一个纯本地Android NDK应用程序,需要检索以下值: android.os.Build.MODEL 不幸的是,我找不到很好的例子来说明如何做到这一点 NDK不是要取代基于Java的API,而是对其进行补充。要获得构建,您必须在C/C++中找到它的私有实现,或者通过JNI从Java提供信息 伪代码: android_main(struct *android_app){ JNIEnv *env = android_app->activity->env; jcla
不幸的是,我找不到很好的例子来说明如何做到这一点 NDK不是要取代基于Java的API,而是对其进行补充。要获得构建,您必须在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跳转到本机。这些值很容易在本机代码中通过
中定义的接口获得,该接口自第一个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).
#包括
//
//公共代码在中定义http://developer.android.com/reference/java/lang/System.html#getProperty(java.lang.String)。
//以下代码在中定义https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/os/Build.java。
//带有*的项目旨在向最终用户显示。
//
#定义ANDROID_OS_BUILD_VERSION_RELEASE“ro.BUILD.VERSION.RELEASE”//*用户可见的版本字符串。例如,“1.0”或“3.4b5”。
#定义ANDROID_OS_BUILD_VERSION_INCREMENTAL“ro.BUILD.VERSION.INCREMENTAL”//底层源代码管理用于表示此构建的内部值。
#定义ANDROID_OS_BUILD_VERSION_代码名“ro.BUILD.VERSION.CODENAME”//当前开发代码名,如果这是发布版本,则定义字符串“REL”。
#定义ANDROID_OS_BUILD_VERSION_SDK“ro.BUILD.VERSION.SDK”//框架的用户可见SDK版本。
#定义ANDROID_OS_BUILD_MODEL“ro.product.MODEL”//*最终产品的最终用户可见名称。。
#定义ANDROID_OS_BUILD_MANUFACTURER“ro.product.MANUFACTURER”//产品/硬件的制造商。
#定义ANDROID_OS_BUILD_BOARD“ro.product.BOARD”//底层板的名称,如“goldfish”。
#定义ANDROID_OS_BUILD_BRAND“ro.product.BRAND”//为软件定制的品牌(如运营商),如有。
#定义ANDROID_OS_BUILD_DEVICE“ro.product.DEVICE”//工业设计的名称。
#定义ANDROID_OS_BUILD_PRODUCT“ro.PRODUCT.name”//整个产品的名称。
#定义ANDROID_OS_BUILD_HARDWARE“ro.HARDWARE”//硬件的名称(从内核命令行或/proc)。
#定义ANDROID_OS_BUILD_CPU_ABI“ro.product.CPU.ABI”//本机代码的指令集名称(CPU类型+ABI约定)。
#定义ANDROID_OS_BUILD_CPU_ABI2“ro.product.CPU.ABI2”//本机代码的第二个指令集(CPU类型+ABI约定)的名称。
#定义ANDROID_OS_BUILD_DISPLAY“ro.BUILD.DISPLAY.id”//*一个用于向用户显示的构建id字符串。
#定义ANDROID_OS_BUILD_HOST“ro.BUILD.HOST”
#定义ANDROID_OS_BUILD_用户“ro.BUILD.USER”
#定义ANDROID_OS_BUILD_ID“ro.BUILD.ID”//变更列表编号或类似“M4-rc20”的标签。
#定义ANDROID_OS_BUILD_TYPE“ro.BUILD.TYPE”//构建类型,如“user”或“eng”。
#定义ANDROID_OS_BUILD_标记“ro.BUILD.TAGS”//逗号分隔的标记来描述构建,如“unsigned,debug”。
#定义ANDROID_OS_BUILD_FINGERPRINT“ro.BUILD.FINGERPRINT”//唯一标识此版本的字符串品牌/产品/设备:发布/标识/版本。增量:类型/标签'。
char model_id[PROP_VALUE_MAX];//属性值最大值。
内伦;
len=uuuu系统u属性u获取(ANDROID操作系统u构建u模型,模型u id);//返回时,len将等于(int)strlen(model_id)。
谢谢。我找不到它的C/C++实现,但是我可以尝试通过JNI从Java检索这些信息。我看到过这样的例子,其中“JNIEnv*”是一个函数参数,后来用于调用方法,如GetMethodID
和CallVoidMethod
,但是我不确定在我的本机方法中可以从何处或如何调用它?我从哪里得到这个JNIEnv*参数?(即我的纯本机应用程序将“android_main”作为我进入纯本机应用程序的入口点,因此我不确定如何调用将JNIEnv*作为参数的函数)啊,这将是一个问题,你从构建中寻找什么样的信息?基本上我需要设备信息,例如Build.MODEL、Build.BRAND、Build.DEVICE和Build.VERSION.release您是使用“android_native_app_glue.h”还是仅使用“native_activity.h”来制作您的应用程序?从那里我可以给你一些伪代码。谢谢。这正是在公开的AndroidNDK文档中缺失的、不明显或不可见的信息。如果您对方法签名和合同感兴趣,这里有一个到来源的链接: