android开发分享Android在不使用数据库的情况下存储数据的方法

本文实例讲述了android在不使用数据库的情况下存储数据的方法。分享给大家供大家参考。具体分析如下: 在有些情况下我们不需要构建数据库,但是却要将一些数据保存起来,等到

android开发分享Android在不使用数据库的情况下存储数据的方法实例讲述了android在不使用数据库的情况下存储数据的方法。分享给大家供大家参考。具体分析如下:

上述就是android开发分享Android在不使用数据库的情况下存储数据的方法的全部内容,如果对大家有所用处且需要了解更多关于Android学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

在有些情况下我们不需要构建数据库,但是却要将一些数据保存起来,等到程序下次运行时调用,那么我们如何做呢?

1. 引用命名空间

  import android.content.sharedpreferences;

2. 定义一个新类pictureglobaldef,用来存储数据,在该类中定义:

  public final static string appsetting = "settingfile";  public final static string default_switch_mode_key="default_switch_mode";  public static boolean switch_open = false;

3. 在要引用数据switch_open 的地方:

  sharedpreferences settingviewmode = getsharedpreferences(   picturenoteglobaldef.appsetting, 0);  boolean bswitch = settingviewmode.getboolean(   picturenoteglobaldef.default_switch_mode_key ,   picturenoteglobaldef.switch_open );

4. 在要保存数据switch_open 的地方:

  picturenoteglobaldef.switch_open = bswitch;  sharedpreferences settingviewmode = getsharedpreferences(appsetting,0);  sharedpreferences.editor editor = settingviewmode.edit();  editor.putboolean(picturenoteglobaldef.default_switch_mode_key ,   picturenoteglobaldef.switch_open );   editor.commit();

5. 读,写其他应用程序的sharedpreferences

有些时候,我们需要读写其他应用程序的sharedpreferences,这时应该怎么办呢?

读取其他应用程序的sharedpreferences关键是获得其他应用程序的context:

  context tempcontext = null;  tempcontext = createpackagecontext("a.b",context.context_ignore_security);  //此处a.b表示该应用的包名

这样就获取了其他应用程序的context了

获取到context之后,就可以使用该context的getsharedpreferences方法获取shaerdpreferences对象,从而按照1-4的方法进行读写数据了。

希望android开发分享Android在不使用数据库的情况下存储数据的方法所述对大家的android程序设计有所帮助。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/940897.html

(0)
上一篇 2021年11月14日
下一篇 2021年11月14日

精彩推荐