前提
我们在androidP及之前的版本,平台侧及应用层开发习惯于通过调用(或者反射)SystemProperties系统API的方式进行系统属性的读写。Android R以后,平台侧代码采用了一种将系统属性封装成类方法的形式供开发者调用。
Android R以前读写属性
1:mk文件或者system.prop文件新增属性
mk文件:
PRODUCT_PROPERTY_OVERRIDES += persist.vendor.usb.config=adb
*.prop文件
persist.vendor.usb.config=true
2:给属性配置上下文
persist.vendor.usb.config u:object_r:usb_prop:s0
3:通过SystemProperties进行读写
SystemProperties.set("persist.vendor.usb.config", "adb,mtp");
String value = SystemProperties.get("persist.vendor.usb.config");
Android R以后读写属性:
例如TelephonyManager.java为例,读写属性:gsm.sim.operator.iso-country
1:设置sim卡国家码
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
public void setSimCountryIsoForPhone(int phoneId, String iso) {
if (SubscriptionManager.isValidPhoneId(phoneId)) {
List<String> newList = updateTelephonyProperty(
TelephonyProperties.icc_operator_iso_country(), phoneId, iso);
TelephonyProperties.icc_operator_iso_country(newList);
}
}
2:读取国家码
@UnsupportedAppUsage
public static String getSimCountryIsoForPhone(int phoneId) {
return getTelephonyProperty(phoneId, TelephonyProperties.icc_operator_iso_country(), "");
}
以上的属性定义在TelephonyProperties
//QSSI.13\frameworks\base\telephony\java\com\android\internal\telephony\TelephonyProperties.java
public interface TelephonyProperties
{
....
/** ISO country code equivalent for the SIM provider's country code*/
static