美文网首页
Android 缓存篇之系统缓存

Android 缓存篇之系统缓存

作者: 牧区叔叔 | 来源:发表于2021-01-11 18:50 被阅读0次

案例场景:目前没有,写一个记住账号密码吧,记住密码一般都是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;
}

相关文章

网友评论

      本文标题:Android 缓存篇之系统缓存

      本文链接:https://www.haomeiwen.com/subject/togdaktx.html