如何检查是否共享preferences文件存在与否 [英] How to check if SharedPreferences file exists or not

查看:101
本文介绍了如何检查是否共享preferences文件存在与否的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个机器人共享preferences我想知道有没有办法只检查是否preferences文件是否存在。

I am looking an android Shared Preferences and I am wondering is there a way to just check if the preferences file exists.

   SharedPreferences  mySharedPreferences ; 
mySharedPreferences=getSharedPreferences("Name_of_your_preference",mode);

这上面code使我相信​​Name_of_Your_ preferene存储为文件或某种容器将包含preferences。

This above code leads me to believe that "Name_of_Your_preferene" is stored as a file or some sort of container that will contain your preferences.

我想知道有没有去检查,如果它存在与否。当用户加载了一个活动我想所有的设置保存到该文件中一些默认值(关闭所有的设置)。不过,我只希望这样做,如果他们要在页面的第一次。

I am wondering is there away to check if this exists or not. When a user loads up an activity I want to save all the settings into this file with some default values(off for all settings). However I only want to do this if they are going to the page for the first time.

否则,如果我每次都做这样的事情在页面加载

Otherwise if I would do something like this every time the page loads up

SharedPreferences.Editor editor= mySharedPreferences.edit();

/* now store your primitive type values. In this case it is true, 1f and Hello! World  */

editor.putBolean("myBoolean",true);

editor.putFloat("myFloat",1f);

editor.putString("myString"," Hello! World");

我猜测它甚至会覆盖所有设置,他们成立的。

I am guessing it would override all settings even ones they set.

推荐答案

的共享preferences保存在一个XML文件中。你可以找到它在/data/data/your_application_package/shared_$p$pfs/Name_of_your_$p$pference.xml

The SharedPreferences are saved in a xml file. You can find it in /data/data/your_application_package/shared_prefs/Name_of_your_preference.xml

要检查是否共享preferences'Name_of_your_ preference'存在:

To check if the SharedPreferences 'Name_of_your_preference' exist :

File f = new File(
"/data/data/your_application_package/shared_prefs/Name_of_your_preference.xml");
if (f.exists())
    Log.d("TAG", "SharedPreferences Name_of_your_preference : exist");
else
    Log.d("TAG", "Setup default preferences");

问候。

这篇关于如何检查是否共享preferences文件存在与否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆