如何保持对Android中的应用程序的信息? [英] How to keep information about an app in Android?
问题描述
我想知道,如果它能够保持在约例如当一个应用程序的信息。
I want to know if its possible to keep information about an app in a while for example.
我有一个访问该文件,并获取有关用户所做的选择信息的应用程序。例如:
I have an app that access this file and get information about the choices that the user have made. For example:
我有很多事件(事件是一个模型)一个按钮,我想知道,即使在应用程序重新启动的按钮,如果用户点击。
I have a button for many events (event is a model), and i want to know if the user clicked in the button even after the application restarts.
我知道这是可以保持大约登录名和密码信息。可以做这样的事情与其他信息?
I know that it is possible to keep information about login and password. Is possible to do something like this with other information?
推荐答案
使用共享preferences 。像这样:
创建这些方法的使用,或者只是使用内容的方法里面,只要你想要的:
Create these methods for use, or just use the content inside of the methods whenever you want:
public String getPrefValue()
{
SharedPreferences sp = getSharedPreferences("preferenceName", 0);
String str = sp.getString("myStore","TheDefaultValueIfNoValueFoundOfThisKey");
return str;
}
public void writeToPref(String thePreference)
{
SharedPreferences.Editor pref =getSharedPreferences("preferenceName",0).edit();
pref.putString("myStore", thePreference);
pref.commit();
}
您可以打电话给他们这样的:
You could call them like this:
// when they click the button:
writeToPref("theyClickedTheButton");
if (getPrefValue().equals("theyClickedTheButton"))
{
// they have clicked the button
}
else if (getPrefValue().equals("TheDefaultValueIfNoValueFoundOfThisKey"))
{
// this preference has not been created (have not clicked the button)
}
else
{
// this preference has been created, but they have not clicked the button
}
的code说明:
Explanation of the code:
preferenceName
是preference您提到这个名字,因此具有每次访问的具体时间$是相同的p $ pference。例如:密码
,theirSettings
"preferenceName"
is the name of the preference you're referring to and therefore has to be the same every time you access that specific preference. eg: "password"
, "theirSettings"
的MyStore指的是一个具体的字符串
存储在preference他们可以是多个。
例如:你有preference theirSettings
,以及随后的MyStore
可能是的声音preFS
,颜色preFS
,语言
等。
"myStore" refers to a specific String
stored in that preference and their can be multiple.
eg: you have preference "theirSettings"
, well then "myStore"
could be "soundPrefs"
, "colourPrefs"
, "language"
, etc.
注意::您可以使用布尔做到这一点
,整数
等
所有你需要做的是改变字符串
存储和读取到布尔
,或任何类型你想要的。
All you have to do is change the String
storing and reading to boolean
, or whatever type you want.
这篇关于如何保持对Android中的应用程序的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!