冲刺六

今天是冲刺的第六天,主要学习了安卓永久性储存和读取用户ID。

遇到的困难:暂无。

解决思路:利用上下问获取一个SharedPreferences对象,进而对用户ID进行储存和访问。

明天计划完成的任务:实现用户的收藏

附上编写的代码:

StoreId.java

package com.itheima.cloudnotes.store;

import android.content.Context;
import android.content.SharedPreferences;

import static android.content.Context.MODE_PRIVATE;

public class StoreId
{
    private  Context context;
    public StoreId(Context context)
    {
        this.context=context;
    }

    public void WiteId(int id)
    {
        SharedPreferences.Editor editor    =context.getSharedPreferences("user_id",MODE_PRIVATE).edit();
        editor.putInt("userId",id);
        editor.apply();
    }

    public void ReadId()
    {
        SharedPreferences pref=    context.getSharedPreferences("user_id",    MODE_PRIVATE);
        int    userId    =    pref.getInt("userId",0);
    }

}
原文地址:https://www.cnblogs.com/weixiao1717/p/13084580.html