Android 数据保存到系统库及系统配置修改

前言

生成的软件识别唯一码想保存到本地,卸载重新安装后根据唯一码进行设备识别。经过查询发现可以使用Settings系统库简单实现。

使用方法

使用方法类似SharePreference,以键值对key-value的形式进行数据存储,将数据内容保存到系统配置文件中,程序卸载安装不影响系统文件,仍然能获取到之前保存的数据内容。

系统配置文件的路径为:/data/data/com.android.providers.settings/databases/

1. 数据保存:

//设置系统配置文件中的配置数据,第一个参数固定,通过上下文获取ContentResolver,第二个参数是要保存数据的Key值,第三个参数是要保存的数据即value值。
Settings.System.putString(getContentResolver(), "key值", "保存数据value值");

2. 数据获取:

//获取系统配置文件中的数据,第一个参数固定,通过上下文获取ContentResolver,第二个参数是已经保存的数据的Key值,通过key获取对应value值。
String value = Settings.System.getString(getContentResolver(), "key值");

3. 配置清单文件权限及权限获取:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
//申请权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
   
    if (!Settings.System.canWrite(context)) {
   
        Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
        intent.setData(Uri.parse("package:" + context.getPackageName()));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}

系统配置

通过Settings我们还可以修改系统中的默认值,例如修改亮度:

private static final int MIN_SCREEN_TIMEOUT_VALUE = 15000;
//修改系统灭屏时间为15秒
Settings.System.putInt(getApplicationContext().getContentResolver(),"screen_off_timeout", MIN_SCREEN_TIMEOUT_VALUE);

深入了解

<?xml version="1.0" encoding="utf-8"?>
<!--
/**
 * Copyright (c) 2009, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
-->
<resources>
    <bool name="def_dim_screen">true</bool>
    <integer name="def_screen_off_timeout">60000</integer>
    <integer name="def_sleep_timeout">-1</integer>
    <bool name="def_airplane_mode_on">false</bool>
    <bool name="def_theater_mode_on">false</bool>
    <!-- Comma-separated list of bluetooth, wifi, and cell. -->
    <string name="def_airplane_mode_radios" translatable="false">cell,bluetooth,wifi,nfc,wimax</string>
    <string name="airplane_mode_toggleable_radios" translatable="false">bluetooth,wifi,nfc</string>
    <string name="def_bluetooth_disabled_profiles" translatable="false">0</string>
    <bool name="def_auto_time">true</bool>
    <bool name="def_auto_time_zone">true</bool>
    <bool name="def_accelerometer_rotation">false</bool
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值