Android的共享preferences,如何拯救一个简单的int变量变量、简单、Android、preferences

2023-09-12 22:31:12 作者:学不会伪装

我想在最后一小时,以节省整在我的Andr​​oid应用程序。我看,这可以通过使用共享preferences来完成。但是我不明白为什么它似乎很混乱的话。

我怎么能简单地保存一个int变量?然后,当我再次运行应用程序,我怎么能再次与这个变量进行交互?

解决方案

 共享preferences SP = getShared preferences(your_ preFS,Activity.MODE_PRIVATE );
共享preferences.Editor编辑器= sp.edit();
editor.putInt(your_int_key,yourIntValue);
editor.commit();
 

在你可以得到它的:

 共享preferences SP = getShared preferences(your_ preFS,Activity.MODE_PRIVATE);
 INT myIntValue = sp.getInt(your_int_key,-1);
 

共享preference 接口,您可以访问到一个XML文件,以及简单的方式通过编辑器修改它。该文件存储在 /data/data/com.your.package/shared_$p$pfs / 键,就可以通过这个访问onlu 共享preference API

Android SharedPreferences

I am trying for the last hour to save an integer in my Android application. I read that this can be done using the SharedPreferences. However i dont understand why it seems so confusing to do so.

How can i simply save an int variable ? And then when i run the application again , how can i interact with this variable again?

解决方案

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("your_int_key", yourIntValue);
editor.commit();

the you can get it as:

 SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
 int myIntValue = sp.getInt("your_int_key", -1);

The SharedPreference interface gives you access to the an xml file, and a easy way to modify it through its editor. The file is stored in /data/data/com.your.package/shared_prefs/ and you can access it onlu through this SharedPreference API