如何根据日常基础清除共享偏好值

时间:2017-08-13 09:40:25

标签: android sharedpreferences android-sharedpreferences android-date

我正在开发一个Android项目,我有一个场景,如:

任务1:

我必须获取用户活动计数“当用户启动活动时,必须进行计数并以共享首选项存储。(已成功完成)

任务2:

我必须每天清除用户共享偏好(例如,如果用户在13-08-2017使用该应用并且计数大约为20(这意味着他已经打开了20次应用)。第二天即14-08-2017我希望在编辑器中清除共享首选项值。

我无法完成第二项任务。如何比较上一个和今天的日期并清除共享首选项编辑器?

以下是我的代码,我正在成功计算当前日期:

public class Fragment_Greetings extends Fragment
{
    private SharedPreferences prefs;
    private SharedPreferences.Editor editor;
    private int totalCount;
    Date currentDate;
    String dateValue;
    public Fragment_Greetings()
    {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        // Inflate the layout for this fragment
        View rootView= inflater.inflate(R.layout.fragment_greetings, container, false);

        // code for getting user daily basis count

        // getting the current date
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE,0);
        SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
        String curentDate = df.format(c.getTime());

        // setting up the editor
        prefs = getActivity().getPreferences(getActivity().MODE_PRIVATE);
        editor = prefs.edit();


        totalCount = prefs.getInt("counter", 0);
        totalCount++;
        editor.putInt("counter", totalCount);

        editor.commit();
        //System.out.println("Total Application counter Reach to :"+totalCount);

        Toast.makeText(getActivity(), "Hello user , you have entered into the app for "+totalCount+" th time on "+curentDate, Toast.LENGTH_SHORT).show();
        return rootView;
    }

}

0 个答案:

没有答案