安卓代码获取系统属性值

安卓代码获取系统属性值

前言

大家可能知道,使用adb shell getprop命令可以直接获取系统属性值,但有时候需要在JAVA代码中获取系统属性,接下来说一下如何实现。

代码实现

在build.gradle的android下中添加如下代码,以找到android.os.SystemProperties类:

String SDK_DIR = System.getenv("ANDROID_SDK_HOME")
if(SDK_DIR == null) {
    Properties props = new Properties()
    props.load(new FileInputStream(project.rootProject.file("local.properties")))
    SDK_DIR = props.get('sdk.dir')
}
dependencies {
    compileOnly files("${SDK_DIR}/platforms/android-21/data/layoutlib.jar")
}

完整的build.gradle示例文件如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 31
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.testsystemproperty"
        minSdkVersion 14
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    //以下是为了找到android.os.SystemProperties这个隐藏的类
    String SDK_DIR = System.getenv("ANDROID_SDK_HOME")
    if(SDK_DIR == null) {
        Properties props = new Properties()
        props.load(new FileInputStream(project.rootProject.file("local.properties")))
        SDK_DIR = props.get('sdk.dir')
    }
    dependencies {
        compileOnly files("${SDK_DIR}/platforms/android-21/data/layoutlib.jar")
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

在代码中需要获取系统属性的地方调用android.os.SystemProperties类的get接口获取系统属性值,以下为示例代码:

package com.example.testsystemproperty;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static String propertyName = "ro.system.build.date"; //根据需要获取的属性名称修改
    private TextView textView1;
    private TextView textView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView1 = findViewById(R.id.tv1);
        textView2 = findViewById(R.id.tv2);
        textView1.setText("propertyName:" + propertyName);
        textView2.setText("propertyValue:" + android.os.SystemProperties.get(propertyName,""));
    }
}

最后在安卓设备运行一下查看效果:
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值