当前位置: 移动技术网 > IT编程>移动开发>Android > Android在不使用数据库的情况下存储数据的方法

Android在不使用数据库的情况下存储数据的方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

workwin破解版,jianguo,董佳妮

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

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

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程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网