系统:Android10.0
设备: FireFly RK3399 (ROC-RK3399-PC-PLUS)
上一节介绍了属性操作的各个API, 本章节通过实战代码来演示如何应用属性。
目标:
1,在代码中获取当前系统的版本
2,为adbd设置tcp连接的端口,并使其生效, 此处会操作属性ctrl.start和ctrl.stop
代码文件:
Android.mk
prop_test.cpp
实例代码
mytest/property_test$ vim prop_test.cpp:
- #include
- #include
-
- // for c
- #include
- #define LOG_TAG "prop_test"
- #include
-
- #include
-
- // for adb
- #define ADBD_TCP_DEFAULT_PORT 15000
- #define PROP_ADBD_TCP_PORT "service.adb.tcp.port"
-
- using namespace std;
-
- void print_android_version()
- {
- char android_version[PROPERTY_VALUE_MAX];
- property_get("ro.build.version.release", android_version, "");
-
- ALOGD("android version : %s", android_version);
-
- }
-
- void set_adbd_tcp_port(unsigned int port){
- if(port <= 10000)
- port = ADBD_TCP_DEFAULT_PORT;
- property_set(PROP_ADBD_TCP_PORT, std::to_string(port).c_str());
-
-
- }
-
- void CplusTestPropString()
- {
-
- const char* test_props[] = {
- "ro.product.name",
- "ro.product.model",
- "ro.product.device",
- };
-
- for (const auto& prop : test_props) {
- std::string value = std::string(prop) + "=" + android::base::GetProperty(prop, "");
- ALOGD("CPlUS API--%s", value.c_str());
- }
-
- }
-
-
-
- int main(int argc, char *argv[])
- {
- print_android_version();
- CplusTestPropString();
-
- set_adbd_tcp_port(0);
- property_set("ctrl.stop", "adbd");
- property_set("ctrl.start", "adbd");
-
- return 0;
- }
mytest/property_test$ vim Android.mk
- LOCAL_PATH:= $(call my-dir)
- include $(CLEAR_VARS)
-
- LOCAL_CFLAGS += \
- -Wno-error \
- -Wno-unused-parameter
-
-
- LOCAL_SHARED_LIBRARIES := \
- libbase \
- libcutils \
- liblog \
-
-
-
- LOCAL_SRC_FILES:= prop_test.cpp
-
- LOCAL_MODULE:= prop_test
-
- include $(BUILD_EXECUTABLE)
编译:
rk3399_Android10.0$ source FFTools/build.s
rk3399_Android10.0$ lunch qh100_rk3399-userdebug
rk3399_Android10.0$ mmma mytest/property_test/
生成的目标文件:
[100% 540/540] Install: out/target/product/qh100_rk3399/system/bin/prop_test
通过adb push将文件推送到/system/bin/目录下进行运行。
- qh100_rk3399:/ # logcat -s property_test
- --------- beginning of main
- 08-17 12:20:24.812 1894 1894 D property_test: android version : 10
- 08-17 12:20:24.812 1894 1894 D property_test: CPlUS API--ro.product.name=qh100_rk3399
- 08-17 12:20:24.812 1894 1894 D property_test: CPlUS API--ro.product.model=qh100
- 08-17 12:20:24.812 1894 1894 D property_test: CPlUS API--ro.product.device=qh100_rk3399
目标: 1, 通过Build类获取到相关属性
2, 通过反射机制获取到属性
PropertyTest\app\src\main\java\com\qh\propertytest\MainActivity.java
- package com.qh.propertytest;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
-
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- private Button btn_build_info;
- private Button btn_get_prop;
- private Button btn_set_prop;
- private TextView tv_show;
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- initUI();
- }
-
- private void initUI() {
- btn_build_info= (Button)findViewById(R.id.btn_build_info);
- btn_build_info.setOnClickListener(this);
- btn_get_prop= (Button)findViewById(R.id.btn_get_prop);
- btn_get_prop.setOnClickListener(this);
- btn_set_prop = (Button)findViewById(R.id.btn_set_prop);
- btn_set_prop.setOnClickListener(this);
- tv_show= (TextView) findViewById(R.id.tv_show);
-
- }
-
- @Override
- public void onClick(View view) {
- switch (view.getId()){
- case R.id.btn_build_info:
- tv_show.setText(PropertyUitls.getBuildDeviceListing());
-
- break;
- case R.id.btn_get_prop:
- tv_show.setText(PropertyUitls.getPropertyListing());
- break;
- case R.id.btn_set_prop:
- PropertyUitls.setProperty("my.second.name", "lilei");
- tv_show.setText(PropertyUitls.getProperty("my.second.name", ""));
- break;
- }
- }
- }
PropertyTest\app\src\main\java\com\qh\propertytest\PropertyUitls.java
- package com.qh.propertytest;
-
- import android.os.Build;
-
- import java.lang.reflect.Method;
-
- public class PropertyUitls {
- private static final int PROP_VALUE_MAX = 91;
-
- public static String getProperty(String key, String defaultValue) {
- String value = defaultValue;
- try {
- Class> c = Class.forName("android.os.SystemProperties");
- Method get = c.getMethod("get", String.class, String.class);
- value = (String)(get.invoke(c, key, defaultValue));
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- return value;
- }
- }
- public static void setProperty(String key, String val) {
-
- if (val != null && !val.startsWith("ro.") && val.length() > PROP_VALUE_MAX) {
- throw new IllegalArgumentException("value of system property '" + key
- + "' is longer than " + PROP_VALUE_MAX + " characters: " + val);
- }
- try {
- Class> c = Class.forName("android.os.SystemProperties");
- Method set = c.getMethod("set", String.class, String.class);
- set.invoke(c, key, val);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- }
- }
-
- public static String getBuildDeviceListing() {
- return "Build.PRODUCT: " + Build.PRODUCT + "\n" +
- "Build.MANUFACTURER: " + Build.MANUFACTURER + "\n" +
- "Build.BRAND: " + Build.BRAND + "\n" +
- "Build.DEVICE: " + Build.DEVICE + "\n" +
- "Build.MODEL: " + Build.MODEL + "\n" +
- "Build.HARDWARE: " + Build.HARDWARE + "\n" +
- "Build.FINGERPRINT: " + Build.FINGERPRINT + "\n" +
- "Build.TAGS: " + Build.TAGS + "\n";
- }
-
- public static String getPropertyListing() {
- return "ro.hardware: " + getProperty("ro.hardware", "") + "\n" +
- "ro.product.model: " + getProperty("ro.product.model", "") + "\n" +
- "ro.product.manufacturer: " + getProperty("ro.product.manufacturer", "") + "\n" +
- "ro.product.board: " + getProperty("ro.product.board", "") + "\n" +
- "init.svc.adbd: " + getProperty("init.svc.adbd", "") + "\n" ;
- }
-
- }
点击BUILDSHOW按钮的结果

点击PROP_SHOW按钮的结果

点击PROP_SET按钮的结果