若有获取Context的其他方法,还请走过路过的大佬不吝赐教.

Android原生涉及到了众多属性及默认值,其中有部分就存储在Settings数据库中,地址如下:       

Android/frameworks/base/core/java/android/provider/Settings.java 

譬如在原生设置中的大部分开关就和此数据库息息相关,在调试过程中,我们有很多可能会绕过原生设置,直接修改数据库以达到目的.

才疏学浅的归纳出我所知道的两种方法:

①利用Context设置.

②利用ADB指令设置.

那么在可获得Context的情况下,可直接按照常规方式修改.

Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 1);

但在很多情况下,无法获取Context,那么我们如何获取Context就成了难点.

学习到了两种方式,粘贴出来供参考:

    private Context getContext() {
        if (Looper.myLooper() == null) {
            Looper.prepare();
        }

        Context topContext = null;
        try {
            topContext = ActivityThread.currentApplication().createPackageContext(ActivityThread.currentPackageName()
                    , Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        if (Looper.myLooper() == null) {
            Looper.loop();
        }
        return topContext;
    }
    public Context getContext() {
        if (Looper.myLooper() == null) {
            Looper.prepare();
        }
        ActivityThread thread = ActivityThread.systemMain();
        Context context = thread.getSystemContext();
        if (Looper.myLooper() == null) {
            Looper.loop();
        }
        return context;
    }

方法是跟大佬学习,他更推荐用上述第一种.但在实测中,在日常系统运行阶段调用可正常试用,但在Android开机阶段调用有概率会出现空指针的问题,进而导致进入Recover阶段.
使用有风险,添加需谨慎!!!

若有获取Context的其他方法,还请走过路过的大佬不吝赐教.

第二种方式就是通过ADB指令去设置数据库 .

ADB 设置Settings数据库值                 

adb shell settings put global device_provisioning_mobile_data 1

ADB 获取Settings数据库值

 adb shell settings get global device_provisioning_mobile_data

既然可以通过ADB去设置,同理就可以通过设置标,继而将ADB指令通过rc文件去执行.实现如下

Android/system/core/rootdir/init.rc

on property:sys.boot_completed=1 && property:persist.vendor.test.flag=1
exec u:r:shell:s0 root root -- /system/bin/settings put Secure accessibility_display_daltonizer_enabled 1

sys.boot_completed = 1 表示 系统启动.

系统启动后,当persist.vendor.test.flag=1时,就会触发下述的指令,继而修改数据库的值.

这种方法暂时没发现问题,可以解决无法获取Context导致无法修改数据库的窘境.

若有获取Context的其他方法,还请走过路过的大佬不吝赐教.

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐