• Android中Settings默认值的设置


    设置Settings的默认值有两种方式

    A. 在获取是有一个接口可以进行默认值设置,当数据库中查询不到该值时,就会返回传入的默认值。

    public static int getInt(ContentResolver cr, String name, int def) {
                return getIntForUser(cr, name, def, cr.getUserId());
            }

    其中int def就是要写入的默认值。

    B. 在SettingProvider中进行设置,也就是写入到数据库中

    首先,在SettingsProvider/res/values/defaults.xml中写入Settings中的值对应的默认值,例如:

    ./packages/SettingsProvider/res/values/defaults.xml:40:    <bool name="def_wifi_display_on">false</bool>

    然后在DatabaseHelper.java中创建完数据库进行默认值的插入

    ./packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java:2559:            loadBooleanSetting(stmt, Settings.Global.WIFI_DISPLAY_ON,R.bool.def_wifi_display_on);

    该插入的流程为:

    onCreate(SQLiteDatabase db) -> loadSettings(db) -> loadSystemSettings(db);loadSecureSettings(db);loadGlobalSettings(db);

    源码如下:

    public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE system (" +
                        "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                        "name TEXT UNIQUE ON CONFLICT REPLACE," +
                        "value TEXT" +
                        ");");
            db.execSQL("CREATE INDEX systemIndex1 ON system (name);");

            createSecureTable(db);

            // Only create the global table for the singleton 'owner/system' user
            if (mUserHandle == UserHandle.USER_SYSTEM) {
                createGlobalTable(db);
            }

            db.execSQL("CREATE TABLE bluetooth_devices (" +
                        "_id INTEGER PRIMARY KEY," +
                        "name TEXT," +
                        "addr TEXT," +
                        "channel INTEGER," +
                        "type INTEGER" +
                        ");");

            db.execSQL("CREATE TABLE bookmarks (" +
                        "_id INTEGER PRIMARY KEY," +
                        "title TEXT," +
                        "folder TEXT," +
                        "intent TEXT," +
                        "shortcut INTEGER," +
                        "ordering INTEGER" +
                        ");");

            db.execSQL("CREATE INDEX bookmarksIndex1 ON bookmarks (folder);");
            db.execSQL("CREATE INDEX bookmarksIndex2 ON bookmarks (shortcut);");

            // Populate bookmarks table with initial bookmarks
            boolean onlyCore = false;
            try {
                onlyCore = IPackageManager.Stub.asInterface(ServiceManager.getService(
                        "package")).isOnlyCoreApps();
            } catch (RemoteException e) {
            }
            if (!onlyCore) {
                loadBookmarks(db);
            }

            // Load initial volume levels into DB
            loadVolumeLevels(db);

            // Load inital settings values
            loadSettings(db);
        }

    private void loadSettings(SQLiteDatabase db) {
            loadSystemSettings(db);
            loadSecureSettings(db);
            // The global table only exists for the 'owner/system' user
            if (mUserHandle == UserHandle.USER_SYSTEM) {
                loadGlobalSettings(db);
            }
        }

    private void loadGlobalSettings(SQLiteDatabase db) {
            SQLiteStatement stmt = null;
            try {
                stmt = db.compileStatement("INSERT OR IGNORE INTO global(name,value)"
                        + " VALUES(?,?);");

                // --- Previously in 'system'
                loadBooleanSetting(stmt, Settings.Global.AIRPLANE_MODE_ON,
                        R.bool.def_airplane_mode_on);

                loadBooleanSetting(stmt, Settings.Global.THEATER_MODE_ON,
                        R.bool.def_theater_mode_on);

                loadStringSetting(stmt, Settings.Global.AIRPLANE_MODE_RADIOS,
                        R.string.def_airplane_mode_radios);

                loadStringSetting(stmt, Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
                        R.string.airplane_mode_toggleable_radios);

                loadBooleanSetting(stmt, Settings.Global.ASSISTED_GPS_ENABLED,
                        R.bool.assisted_gps_enabled);
    .....
    loadBooleanSetting(stmt, Settings.Global.WIFI_DISPLAY_ON,
                        R.bool.def_wifi_display_on);
    ......

    }
  • 相关阅读:
    SharePoint 2013 开启訪问请求
    鸟哥Linux私房菜知识点总结3到5章
    火狐浏览器中加入httprequest的方法
    xhprof安装&amp;&amp;使用
    第一篇、Android Supersu 权限管理定制,隐藏过滤权限,指定APP最高权限
    Android 推断程序在手机中是否是活动状态或者正在执行状态
    【Java并发编程实战】—–synchronized
    24岁菜鸟,能一个人撑起App开发吗
    技术与经济之八:关于优惠与红包
    关于notepad++如何自动补全标签的问题
  • 原文地址:https://www.cnblogs.com/wangzhe1635/p/9811999.html
Copyright © 2020-2023  润新知