• Android系统10 RK3399 init进程启动(三十七) 属性代码编程


    配套系列教学视频链接:

          安卓系列教程之ROM系统开发-百问100ask

    说明

    系统:Android10.0

    设备: FireFly RK3399 (ROC-RK3399-PC-PLUS)

    前言

    上一节介绍了属性操作的各个API, 本章节通过实战代码来演示如何应用属性。


    一, C/C++代码

    目标:

    1,在代码中获取当前系统的版本

    2,为adbd设置tcp连接的端口,并使其生效, 此处会操作属性ctrl.start和ctrl.stop

    代码文件: 

    Android.mk 

    prop_test.cpp

    实例代码

    mytest/property_test$ vim prop_test.cpp:

    1. #include
    2. #include
    3. // for c
    4. #include
    5. #define LOG_TAG "prop_test"
    6. #include
    7. #include
    8. // for adb
    9. #define ADBD_TCP_DEFAULT_PORT 15000
    10. #define PROP_ADBD_TCP_PORT "service.adb.tcp.port"
    11. using namespace std;
    12. void print_android_version()
    13. {
    14. char android_version[PROPERTY_VALUE_MAX];
    15. property_get("ro.build.version.release", android_version, "");
    16. ALOGD("android version : %s", android_version);
    17. }
    18. void set_adbd_tcp_port(unsigned int port){
    19. if(port <= 10000)
    20. port = ADBD_TCP_DEFAULT_PORT;
    21. property_set(PROP_ADBD_TCP_PORT, std::to_string(port).c_str());
    22. }
    23. void CplusTestPropString()
    24. {
    25. const char* test_props[] = {
    26. "ro.product.name",
    27. "ro.product.model",
    28. "ro.product.device",
    29. };
    30. for (const auto& prop : test_props) {
    31. std::string value = std::string(prop) + "=" + android::base::GetProperty(prop, "");
    32. ALOGD("CPlUS API--%s", value.c_str());
    33. }
    34. }
    35. int main(int argc, char *argv[])
    36. {
    37. print_android_version();
    38. CplusTestPropString();
    39. set_adbd_tcp_port(0);
    40. property_set("ctrl.stop", "adbd");
    41. property_set("ctrl.start", "adbd");
    42. return 0;
    43. }

    mytest/property_test$ vim Android.mk

    1. LOCAL_PATH:= $(call my-dir)
    2. include $(CLEAR_VARS)
    3. LOCAL_CFLAGS += \
    4. -Wno-error \
    5. -Wno-unused-parameter
    6. LOCAL_SHARED_LIBRARIES := \
    7. libbase \
    8. libcutils \
    9. liblog \
    10. LOCAL_SRC_FILES:= prop_test.cpp
    11. LOCAL_MODULE:= prop_test
    12. 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/目录下进行运行。

    1. qh100_rk3399:/ # logcat -s property_test
    2. --------- beginning of main
    3. 08-17 12:20:24.812 1894 1894 D property_test: android version : 10
    4. 08-17 12:20:24.812 1894 1894 D property_test: CPlUS API--ro.product.name=qh100_rk3399
    5. 08-17 12:20:24.812 1894 1894 D property_test: CPlUS API--ro.product.model=qh100
    6. 08-17 12:20:24.812 1894 1894 D property_test: CPlUS API--ro.product.device=qh100_rk3399

    二,java代码示例

    目标: 1, 通过Build类获取到相关属性

    2, 通过反射机制获取到属性

    PropertyTest\app\src\main\java\com\qh\propertytest\MainActivity.java

    1. package com.qh.propertytest;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.os.Bundle;
    4. import android.view.View;
    5. import android.widget.Button;
    6. import android.widget.TextView;
    7. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    8. private Button btn_build_info;
    9. private Button btn_get_prop;
    10. private Button btn_set_prop;
    11. private TextView tv_show;
    12. @Override
    13. protected void onCreate(Bundle savedInstanceState) {
    14. super.onCreate(savedInstanceState);
    15. setContentView(R.layout.activity_main);
    16. initUI();
    17. }
    18. private void initUI() {
    19. btn_build_info= (Button)findViewById(R.id.btn_build_info);
    20. btn_build_info.setOnClickListener(this);
    21. btn_get_prop= (Button)findViewById(R.id.btn_get_prop);
    22. btn_get_prop.setOnClickListener(this);
    23. btn_set_prop = (Button)findViewById(R.id.btn_set_prop);
    24. btn_set_prop.setOnClickListener(this);
    25. tv_show= (TextView) findViewById(R.id.tv_show);
    26. }
    27. @Override
    28. public void onClick(View view) {
    29. switch (view.getId()){
    30. case R.id.btn_build_info:
    31. tv_show.setText(PropertyUitls.getBuildDeviceListing());
    32. break;
    33. case R.id.btn_get_prop:
    34. tv_show.setText(PropertyUitls.getPropertyListing());
    35. break;
    36. case R.id.btn_set_prop:
    37. PropertyUitls.setProperty("my.second.name", "lilei");
    38. tv_show.setText(PropertyUitls.getProperty("my.second.name", ""));
    39. break;
    40. }
    41. }
    42. }

    PropertyTest\app\src\main\java\com\qh\propertytest\PropertyUitls.java

    1. package com.qh.propertytest;
    2. import android.os.Build;
    3. import java.lang.reflect.Method;
    4. public class PropertyUitls {
    5. private static final int PROP_VALUE_MAX = 91;
    6. public static String getProperty(String key, String defaultValue) {
    7. String value = defaultValue;
    8. try {
    9. Class c = Class.forName("android.os.SystemProperties");
    10. Method get = c.getMethod("get", String.class, String.class);
    11. value = (String)(get.invoke(c, key, defaultValue));
    12. } catch (Exception e) {
    13. e.printStackTrace();
    14. } finally {
    15. return value;
    16. }
    17. }
    18. public static void setProperty(String key, String val) {
    19. if (val != null && !val.startsWith("ro.") && val.length() > PROP_VALUE_MAX) {
    20. throw new IllegalArgumentException("value of system property '" + key
    21. + "' is longer than " + PROP_VALUE_MAX + " characters: " + val);
    22. }
    23. try {
    24. Class c = Class.forName("android.os.SystemProperties");
    25. Method set = c.getMethod("set", String.class, String.class);
    26. set.invoke(c, key, val);
    27. } catch (Exception e) {
    28. e.printStackTrace();
    29. } finally {
    30. }
    31. }
    32. public static String getBuildDeviceListing() {
    33. return "Build.PRODUCT: " + Build.PRODUCT + "\n" +
    34. "Build.MANUFACTURER: " + Build.MANUFACTURER + "\n" +
    35. "Build.BRAND: " + Build.BRAND + "\n" +
    36. "Build.DEVICE: " + Build.DEVICE + "\n" +
    37. "Build.MODEL: " + Build.MODEL + "\n" +
    38. "Build.HARDWARE: " + Build.HARDWARE + "\n" +
    39. "Build.FINGERPRINT: " + Build.FINGERPRINT + "\n" +
    40. "Build.TAGS: " + Build.TAGS + "\n";
    41. }
    42. public static String getPropertyListing() {
    43. return "ro.hardware: " + getProperty("ro.hardware", "") + "\n" +
    44. "ro.product.model: " + getProperty("ro.product.model", "") + "\n" +
    45. "ro.product.manufacturer: " + getProperty("ro.product.manufacturer", "") + "\n" +
    46. "ro.product.board: " + getProperty("ro.product.board", "") + "\n" +
    47. "init.svc.adbd: " + getProperty("init.svc.adbd", "") + "\n" ;
    48. }
    49. }

    点击BUILDSHOW按钮的结果 

    点击PROP_SHOW按钮的结果

    点击PROP_SET按钮的结果

     

     

     

  • 相关阅读:
    Spectacle/Flameshot/X11 Xlib截屏问题现象及解决方法
    HTML常用基本元素总结
    外汇天眼:外汇交易商常见黑心手法大公开!投资务必留意这5种骗局
    Android Settings 单元测试 | Telephony Network 模块 APN 案例
    数据结构全解(链,数组,栈,树,图)与算法分析(排序,查找,递归,分治,动态规划)
    Zabbix
    安装virtualenvwrapper报错权限不足解决方法(Mac)
    JS多个HTTP请求限制最大并发数
    Spring Boot Actuator介绍
    云原生周刊:Gateway API 1.0.0 发布 | 2023.11.6
  • 原文地址:https://blog.csdn.net/ldswfun/article/details/126374813