APP 获取系统ro属性
引用方法:https://my.oschina.net/chaselinfo/blog/213393?p=1
activity_ main.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java :
package com.example.myapplication0411;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.lang.reflect.Method;
import android.content.Context;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView test = findViewById(R.id.test);
// String model = SystemProperties.get("ro.product.model");
String str0 = SystemPropertiesProxy.get(this,"ro.build.tags");
String str1 = SystemPropertiesProxy.get(this,"ro.build.description");
String str2 = SystemPropertiesProxy.get(this,"ro.build.fingerprint");
test.setText(str0+"\n"+str1+"\n"+str2);
}
}
class SystemPropertiesProxy {
/**
* 根据给定Key获取值.
*
* @return 如果不存在该key则返回空字符串
* @throws IllegalArgumentException 如果key超过32个字符则抛出该异常
*/
public static String get(Context context, String key) throws IllegalArgumentException {
String ret = "";
try {
ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = cl.loadClass("android.os.SystemProperties");
//参数类型
@SuppressWarnings("rawtypes")
Class[] paramTypes = new Class[1];
paramTypes[0] = String.class;
Method get = SystemProperties.getMethod("get", paramTypes);
//参数
Object[] params = new Object[1];
params[0] = new String(key);
ret = (String) get.invoke(SystemProperties, params);
} catch (IllegalArgumentException iAE) {
throw iAE;
} catch (Exception e) {
ret = "";
//TODO
}
return ret;
}
}