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程序设计有所帮助。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android Manifest中meta-data扩展元素数据的配置与获取方式
Android实现获取meta-data和build.gradle的值
Android获取清单文件中的meta-data,解决碰到数值为null的问题
解决webview 第二次调用loadUrl页面不刷新的问题
解决Android WebView拦截url,视频播放加载失败的问题
Android webview加载https链接错误或无响应的解决
网友评论