在实际的开发过程中,Android提供了5种方式存储数据:
1.文件存储数据
2.使用 Sharedpreferences 存储数据
3.SQLite数据库存储数据
4.使用ContentProvider存储数据
5.网络存储数据
首先我们先简单了解下文件、SharedPreferred如何进行数据存储
1.文件存储操作
文件存储一般存储在sdcard或者ROM,当文件存储在ROM上时,如果是存储在除自己私有空间外(data/data/包名/),在其他地方是需要system系统权限的(http://my.oschina.net/zhoulc/blog/119282)。
文件IO流创建比较简单,就不详述了,在进行文件存储的时候注意两个路径方法就行。
存储到ROM:
Context mContext = XXXActivity.this;
mContext.getFilesDir().getAbsolutePath();
获取路径:/data/data/包名/files
存储到sdCard :
首先判断sdcard是否可用
Environment.getExternalStorageState()是否等于Environment.MEDIA_MOUNTED
Environment.getExternalStorageDirectory().getAbsolutePath()
对sdcard目录操作需要添加权限:
< uses-permission android:name ="android.permission.WRITE_EXTERNAL_STORAGE" >
获取路径:/sdcard
拓展下 :支持u盘的移动设备,u盘一般都是挂载mnt/sda/sdal外设上面
顺带说下以前一直纠结的一个问题:
getApplicationContext() 生命周期是整个应用,应用摧毁它才摧毁
Activity.this的context属于activity ,activity 摧毁他就摧毁
2.通过Sharedpreferences进行数据存储
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//存放
//第一个参数是存储时的名称,第二个参数则是文件的打开方式(Context.
MODE_PRIVATE)
SharedPreferences userInfo = getSharedPreferences(PERFS_NAME,
0
);
SharedPreferences.Editor editor = userInfo.edit();
editor.putString(
"userName"
,
"zhoulc"
);
editor.putString(
"passWord"
,
"ds"
);
editor.commit();
//读取
System.out.println(
"##########################################"
);
SharedPreferences readInfo = getSharedPreferences(PERFS_NAME,
0
);
String name = readInfo.getString(
"userName"
,
"no_name"
);
String psword = readInfo.getString(
"passWord"
,
"000000"
);
System.out.println(
"name = "
+name+
" password = "
+psword);
}
|
3.其他几个就不详细介绍了,说下今天的重点ContentProvider
创建一个可对数据库进行操作的ContentProvider我们先要了解涉及到的知识点:
(1)Parcelable(实体类数据一般会用在进程之间通信)
http://my.oschina.net/zhoulc/blog/172163
(2)BaseColumns,这个类只是提供了两个字段,一个是"_id"一个是"_count",便于调用数据库时导致拼写错误,你也可以扩展它,或者自定义这么个,然后直接调用它的常量名,防止写sql语句时把列名拼错。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public
interface
BaseColumns
{
/**
* The unique ID for a row.
*
*/
public
static
final
String _ID =
"_id"
;
/**
* The count of rows in a directory.
*
*/
public
static
final
String _COUNT =
"_count"
;
}
|
5.下面贴一下具体实现代码:
(1)ContentProvider提供者TestContentProvider.apk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package
com.zlc.provider;
import
android.net.Uri;
import
android.os.Parcel;
import
android.os.Parcelable;
import
android.provider.BaseColumns;
public
class
Information
implements
Parcelable,BaseColumns{
public
final
static
String AUTHORITY =
"com.zlc.provider.MyProvider"
;
public
final
static
Uri CONTENT_URI = Uri.parse(
"content://"
+AUTHORITY+
"/informations"
);
//表字段
public
final
static
String INFO_ID =
"info_id"
;
public
final
static
String INFO_NAME =
"info_name"
;
public
final
static
String INFO_AGE =
"info_age"
;
private
String info_id,info_name,info_age;
public
Information(Parcel source){
info_id = source.readString();
info_name = source.readString();
info_age = source.readString();
}
public
String getInfo_id() {
return
info_id;
}
public
void
setInfo_id(String info_id) {
this
.info_id = info_id;
}
public
String getInfo_name() {
return
info_name;
}
public
void
setInfo_name(String info_name) {
this
.info_name = info_name;
}
public
String getInfo_age() {
return
info_age;
}
public
void
setInfo_age(String info_age) {
this
.info_age = info_age;
}
@Override
public
int
describeContents() {
// TODO Auto-generated method stub
return
0
;
}
@Override
public
void
writeToParcel(Parcel dest,
int
flags) {
// TODO Auto-generated method stub
dest.writeString(info_id);
dest.writeString(info_name);
dest.writeString(info_age);
}
public
final
static
Parcelable.Creator
new
Parcelable.Creator
@Override
public
Information createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return
new
Information(source);
}
@Override
public
Information[] newArray(
int
size) {
// TODO Auto-generated method stub
return
new
Information[size];
}
};
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
package
com.zlc.provider;
import
android.content.ContentProvider;
import
android.content.ContentUris;
import
android.content.ContentValues;
import
android.content.Context;
import
android.content.UriMatcher;
import
android.database.Cursor;
import
android.database.sqlite.SQLiteDatabase;
import
android.database.sqlite.SQLiteOpenHelper;
import
android.net.Uri;
import
android.provider.BaseColumns;
import
android.util.Log;
public
class
MyProvider
extends
ContentProvider {
private
SQLiteDatabase sqDb;
private
DatabaseHelper helper;
// 数据库名
private
final
static
String DATABASE_NAME =
"zhoulc.db"
;
// 版本
private
static
final
int
DATABASE_VERSION =
1
;
// 表名
private
static
final
String TABLE_NAME =
"Information"
;
// 创建表的sql语句
private
final
static
String CREATE_TABLE =
"Create table "
+ TABLE_NAME
+
"( "
+Information._ID+
" integer primary key autoincrement,"
+ Information.INFO_ID
+
" TEXT,"
+ Information.INFO_NAME +
" TEXT,"
+ Information.INFO_AGE
+
" TEXT);"
;
// Declaration Datababsehelper
private
static
class
DatabaseHelper
extends
SQLiteOpenHelper {
public
DatabaseHelper(Context context) {
super
(context, DATABASE_NAME,
null
, DATABASE_VERSION);
}
@Override
public
void
onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_TABLE);
}
@Override
public
void
onUpgrade(SQLiteDatabase db,
int
oldVersion,
int
newVersion) {
// TODO Auto-generated method stub
db.execSQL(
"DROP TABLE IF EXISTS"
+ TABLE_NAME);
onCreate(db);
}
}
// UriMatcher add URI
private
static
final
UriMatcher MATCHER =
new
UriMatcher(
UriMatcher.NO_MATCH);
private
final
static
int
INFORMATIONS =
1
;
private
final
static
int
INFORMATION =
2
;
static
{
MATCHER.addURI(Information.AUTHORITY,
"informations"
, INFORMATIONS);
MATCHER.addURI(Information.AUTHORITY,
"informations/#"
, INFORMATION);
}
@Override
public
int
delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
sqDb = helper.getWritableDatabase();
int
count =
0
;
switch
(MATCHER.match(uri)) {
case
INFORMATIONS:
count = sqDb.delete(TABLE_NAME, selection, selectionArgs);
break
;
default
:
throw
new
IllegalArgumentException(
"Unkwon Uri:"
+ uri.toString());
}
return
count;
}
@Override
public
String getType(Uri uri) {
// TODO Auto-generated method stub
switch
(MATCHER.match(uri)) {
case
INFORMATIONS:
return
"vnd.android.cursor.dir/Information"
;
case
INFORMATION:
return
"vnd.android.cursor.item/Information"
;
default
:
throw
new
IllegalArgumentException(
"Unkwon Uri:"
+ uri.toString());
}
}
@Override
public
Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
sqDb = helper.getWritableDatabase();
Uri insertUri =
null
;
long
rowid =
0
;
switch
(MATCHER.match(uri)) {
case
INFORMATIONS:
rowid = sqDb.insert(TABLE_NAME, Information.INFO_ID, values);
insertUri = ContentUris.withAppendedId(uri, rowid);
Log.i(
"zhoulc"
,
"insert record...values:"
+ values.toString());
break
;
default
:
throw
new
IllegalArgumentException(
"Unkwon Uri:"
+ uri.toString());
}
return
insertUri;
}
@Override
public
boolean
onCreate() {
// TODO Auto-generated method stub
helper =
new
DatabaseHelper(getContext());
return
helper ==
null
?
false
:
true
;
}
@Override
public
Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
sqDb = helper.getWritableDatabase();
switch
(MATCHER.match(uri)) {
case
INFORMATIONS:
Cursor cursor = sqDb.query(TABLE_NAME, projection, selection,
selectionArgs,
null
,
null
, sortOrder);
return
cursor;
case
INFORMATION:
//条件查询,
long
id = ContentUris.parseId(uri);
String where = Information._ID +
"="
+ id;
if
(selection !=
null
&& !
""
.equals(selection))
{
where = where +
" and "
+ selection;
}
return
sqDb.query(TABLE_NAME, projection, where, selectionArgs,
null
,
null
, sortOrder);
default
:
throw
new
IllegalArgumentException(
"unknow uri"
+ uri.toString());
}
}
@Override
public
int
update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
sqDb = helper.getWritableDatabase();
int
count =
0
;
switch
(MATCHER.match(uri)) {
case
INFORMATIONS:
count = sqDb.update(TABLE_NAME, values, selection, selectionArgs);
return
count;
default
:
throw
new
IllegalArgumentException(
"unknow uri"
+ uri.toString());
}
}
}
|
在同一个应用本身里面(进程)测试插入数据:MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package
com.zlc.provider;
import
android.app.Activity;
import
android.content.ContentValues;
import
android.content.Context;
import
android.content.pm.ApplicationInfo;
import
android.content.pm.PackageManager;
import
android.content.pm.PackageManager.NameNotFoundException;
import
android.net.Uri;
import
android.os.Bundle;
public
class
MainActivity
extends
Activity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
Context mContext = MainActivity.
this
;
Uri myUri = Information.CONTENT_URI;
&nbs
|