Android-failed to set system property

1. 现象

failed to set system property,无法写属性值的闪退报错

2019-10-09 18:56:20.020 10252-10252/com.fadi.gps_switch W/.fadi.gps_switch: type=1400 audit(0.0:211): avc: denied { write } for name="com.fadi.gps_switch-Ox-iQk-Dkwj06P13_L--fg==" dev="mmcblk0p46" ino=606212 scontext=u:r:system_app:s0 tcontext=u:object_r:apk_data_file:s0 tclass=dir permissive=0

2019-10-09 18:58:14.680 10252-10252/com.fadi.gps_switch W/libc: Unable to set property "persist.fadi.gps" to "true": error code: 0x18
2019-10-09 18:58:14.680 10252-10252/com.fadi.gps_switch D/AndroidRuntime: Shutting down VM
    
    
    --------- beginning of crash
2019-10-09 18:58:14.686 10252-10252/com.fadi.gps_switch E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.fadi.gps_switch, PID: 10252
    java.lang.RuntimeException: failed to set system property
        at android.os.SystemProperties.native_set(Native Method)
        at android.os.SystemProperties.set(SystemProperties.java:196)
        at com.fadi.gps_switch.MainActivity.setGpsPowerSave(MainActivity.java:73)
        at com.fadi.gps_switch.MainActivity.access$000(MainActivity.java:10)
        at com.fadi.gps_switch.MainActivity$1.onCheckedChanged(MainActivity.java:47)
        at android.widget.CompoundButton.setChecked(CompoundButton.java:180)
        at android.support.v7.widget.SwitchCompat.setChecked(SwitchCompat.java:1060)
        at android.support.v7.widget.SwitchCompat.toggle(SwitchCompat.java:1055)
        at android.widget.CompoundButton.performClick(CompoundButton.java:140)
        at android.view.View.performClickInternal(View.java:7123)
        at android.view.View.access$3500(View.java:807)
        at android.view.View$PerformClick.run(View.java:27540)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7486)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)

3.代码调用

    /**
     * adb shell getprop persist.sys.fadi.gps
     */
    private boolean isGpsPowerSaveOn(){
        boolean resulut = false;

        String info = SystemProperties.get(GPS_KEY, "false").trim().toString();
        Log.d(TAG, "info");
        resulut = "true".equals(info);

        return resulut;
    }

    /**
     * adb shell setprop persist.sys.fadi.gps
     */
    private void setGpsPowerSave(boolean value) {
        SystemProperties.set(GPS_KEY, value ? "true" : "false");
    }

3. 分析

上述需满足下述条件:

  1. SE-LINUX
  2. android:sharedUserId=“android.uid.system”
  3. 系统签名

我这边是违反了,属性key为 persist.fadi.gps,需要定义相应的Se-linux权限。

如果不想额外定义,可以修改为persist.sys前缀开头就行;参考依据来源于源码定义

system/sepolicy/private/property_contexts

persist.sys.            u:object_r:system_prop:s0

即修改为 persist.sys.fadi.gps 即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值