最近在学android,发现前面学了,等学完下一个点就忘光了
记录下,方便查找
1.SharedPreferences存储方式
xml格式保存,适合配置数据保存
使用:Context、ContextWrapper类中提供getSharedPreferences(String name, int mode),默认保存位置:data/data/*对应app*/shared_prefs/*xml_name*
name:xml文件名
mode:文件权限
* MODE_PRIVATE:默认,只能本应用读写
* MODE_WORLD_READABLE:能被其他应用读,不能写
* MODE_WORLD_WRITEABLE:能被其他应用读、写
存储
Editor = getSharedPreferenc().edit()//返回Editor对象
Editor .put*Type*(“nodeName”,value)//存储对应数据
Editor.clear()
Editor.remove(string key)
editor.commit()
获取
getSharedPreferences(**,**).get*Type*("nodeName",defalueValue)
getDefaultSharedPreferences: 实际与getSharedPreferences相同,默认了一个xmlname
public static SharedPreferences getDefaultSharedPreferences(Context context) {
return context.getSharedPreferences(getDefaultSharedPreferencesName(context),
getDefaultSharedPreferencesMode());// Context.MODE_PRIVATE
}
2.文件存储
使用io流操作文件
FileInputStream 输入
FileOutputStream输出 位置 app_name/files/file_name
参数name:文件名
mode:打开模式,
MODE_PRIVATE:默认 只能本应用读写 覆盖内容
MODE_APPEND:追加模式 只能本应用读写
MODE_WORLD_READABLE:其他应用可读
MODE_WORLD_WRITEABLE:其他应用可写
读取sdcard文件:
需要AndroidManifest.xml中加入sd卡权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
1.获得sdcard状态:Environment.getExternalStorageState()
equal(MEDIA_MOUNTED);具有读写权限
MEDIA_MOUNTED_READ_ONLY:只读
sd卡目录:Environment.getExternalStorageDirectory()
3.sqlite存储数据
SQLite3的数据引擎
可使用原生sql语言,封装了一些操作
SQLiteDatabase db = Context.OpenCreateDateBase(“sql_name”,mode,factory);//打开创建数据库
db.execSQL(sql);//
insert,update,delete有封装
使用SQLiteOpenHelper
4.ContentProvider:跨应用数据操作
在AndroidManifest.xml中声明
<applicaytion>
<provide android:name="provider_name" android:authorities="..."
</application>
使用继承ContentProvider,并重载其delete()、insert()、query()、update()、getType()、onCreate()方法
其他应用获取数据:
用Context获取到当前的ContentResolver,根据Uri地址和ContentResolver的query方法获取A程序的数据。Uri地址和A程序中AndroidManifest.xml定义的autorities要一致。
5.网络存储
权限<uses-permission android:name="android.permission.INTERNET" />
从网络中获取数据
URL uri = new URL(‘。。。’);
URLConnection uConnection = uri.openConnection();
InputStream is = uConnection.getInputStream();
//InputStream操作获取。。
参考的链接:
Android数据存储五种方式总结:http://www.cnblogs.com/ITtangtang/p/3920916.html
[Android]Android数据的四种存储方式:http://www.cnblogs.com/rond/p/3553156.html
ANDROID开发之SQLite详解:http://www.cnblogs.com/Excellent/archive/2011/11/19/2254888.html