Android 缓存篇之系统缓存

案例场景:目前没有,写一个记住账号密码吧,记住密码一般都是SP缓存,卸载之后就没了,但是如果用系统缓存的话卸载后重新安装还是有数据的。

我分别测试了几个不同版本,因为我是硬件开发,硬件版本5.1.1 ,考虑到手机,就下载了几个不同版本测试


image.png

效果图:
5.1.1 版本


5.1.1系统缓存.gif

通过测试发现5.1.1 版本手机是可以使用的,哪怕你卸载了重新安装时候还是会读取数据的

6.0 版本


6.0系统缓存.gif

通过测试发现6.0 版本手机也可以使用的,就是卸载重新安装后初始化值。

8.0 版本 不支持
10.0 版本 不支持

应该是版本特性问题,我后期找找补上

通过测试我们发现5.1.1 以及 6.0版本是可以使用的, 但是8.0 和 10.0是不行的,应该是权限或者别的吧,有时间在补上,这个需要的大家可以从版本升级那边找找答案,我后期补上这块!

代码示意图:


image.png

代码如下:

权限 我就是忘了这个一直调试不出来~

   <uses-permission android:name="android.permission.READ_SETTINGS" />
   <uses-permission android:name="android.permission.WRITE_SETTINGS" />

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.phone.rmbpws">

    <uses-permission android:name="android.permission.READ_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity2"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity

package com.phone.rmbpws;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {

    private EditText etCnt;
    private EditText etPsw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        etCnt = (EditText) findViewById(R.id.et_cnt);
        etPsw = (EditText) findViewById(R.id.et_psw);

        CommonManage.inIt(this);

        etCnt.setText(NormalConfig.USER);
        etPsw.setText(NormalConfig.PSW);
    }

    private void start() {
        startActivity(new Intent(this, MainActivity2.class));
        finish();
    }

    public void onClickLogin(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                saveLogin();
                break;
        }
    }

    private void saveLogin() {
        String user = etCnt.getText().toString().trim();
        String psw = etPsw.getText().toString().trim();
        if (user.length() == 0) {
            Toast.makeText(this, "Account number cannot be empty", Toast.LENGTH_SHORT).show();
            return;
        }
        if (psw.length() == 0) {
            Toast.makeText(this, "Password cannot be empty", Toast.LENGTH_SHORT).show();
            return;
        }

        CommonManage.cm.save(user, psw);

        Toast.makeText(this, "Successful login", Toast.LENGTH_SHORT).show();
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }
}

activity.xml


image.png
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_cnt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="80dp"
        android:padding="6dp"
        android:text="账号:"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tv_psw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_cnt"
        android:layout_alignLeft="@id/tv_cnt"
        android:layout_marginTop="20dp"
        android:padding="6dp"
        android:text="密码:"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/et_cnt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/tv_cnt"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp"
        android:layout_toRightOf="@id/tv_cnt"
        android:hint="输入账号"
        android:padding="6dp"
        android:textSize="18sp" />

    <EditText
        android:inputType="number"
        android:id="@+id/et_psw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/tv_psw"
        android:layout_alignRight="@id/et_cnt"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/tv_psw"
        android:hint="输入密码"
        android:padding="6dp"
        android:textSize="18sp" />

    <TextView
        android:onClick="onClickLogin"
        android:id="@+id/tv_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/tv_cnt"
        android:layout_alignRight="@id/et_cnt"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="80dp"
        android:gravity="center"
        android:padding="6dp"
        android:text="登录"
        android:textSize="18sp" />

</RelativeLayout>

CommonManage.java

package com.phone.rmbpws;

import android.content.Context;

public class CommonManage {
    public static ConfigManage cm;

    public static void inIt(Context cnt) {
        cm = new ConfigManage(cnt);
    }
}

ConfigManage.java

package com.phone.rmbpws;

import android.content.Context;

public class ConfigManage {
    public static String user = "user";
    public static String psw = "psw";

    private SettingManage setManage;

    public ConfigManage(Context cnt) {
        setManage = new SettingManage(cnt);
        getStaticData();
    }

    private void getStaticData() {
        NormalConfig.USER = setManage.get(user, "admin");
        NormalConfig.PSW = setManage.get(psw, "000");
    }

    public boolean save(String user, String psw) {
        boolean result = false;
        try {
            setManage.set(this.user, user);
            setManage.set(this.psw, psw);

            NormalConfig.USER = user;
            NormalConfig.PSW = psw;
            result = true;
        } catch (Exception e) {
        }
        return result;
    }
}

SettingManage.java

package com.phone.rmbpws;

import android.content.Context;
import android.provider.Settings;

public class SettingManage {
    Context cnt;

    public SettingManage(Context c) {
        cnt = c;
    }

    public int set(String key, String value) {
        int result = 0;
        try {
            Settings.System.putString(cnt.getContentResolver(), key, value);
            result = 1;
        } catch (Exception e) {
        }
        return result;
    }


    public String get(String key, String defaultValue) {
        String result = null;
        try {
            result = Settings.System.getString(cnt.getContentResolver(), key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result == null ? defaultValue : result;
    }
}

NormalConfig.java

package com.phone.rmbpws;

public class NormalConfig {
    public static String USER;
    public static String PSW;

    public static boolean flag;
}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,402评论 6 499
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,377评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,483评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,165评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,176评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,146评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,032评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,896评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,311评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,536评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,696评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,413评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,008评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,815评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,698评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,592评论 2 353

推荐阅读更多精彩内容

  • 这篇博文主要是介绍Android操作系统中,个人汇总的比较重要的一些知识点,在Android的系统四层结构中...
    Android开发_Hua阅读 4,089评论 0 4
  • android 8.0在系统性能和安全性上做了一些优化,而这些API上的改动,对我们软件也带来一部分影响,下面简要...
    Felix_lin阅读 4,522评论 -3 0
  • 1.Android系统漫谈 Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如...
    夜尽雪舞阅读 1,767评论 0 2
  • 推荐指数: 6.0 书籍主旨关键词:特权、焦点、注意力、语言联想、情景联想 观点: 1.统计学现在叫数据分析,社会...
    Jenaral阅读 5,716评论 0 5
  • 昨天,在回家的路上,坐在车里悠哉悠哉地看着三毛的《撒哈拉沙漠的故事》,我被里面的内容深深吸引住了,尽管上学时...
    夜阑晓语阅读 3,784评论 2 9