//实例化SharedPreferences对象(第一步)
SharedPreferences mySharedPreferences= getSharedPreferences("test",
Activity.MODE_PRIVATE);
//实例化SharedPreferences.Editor对象(第二步)
SharedPreferences.Editor editor = mySharedPreferences.edit();
//用putString的方法保存数据
editor.putString("name", "Karl");
editor.putString("habit", "sleep");
//提交当前数据
editor.commit();
//使用toast信息提示框提示成功写入数据
Toast.makeText(this, "数据成功写入SharedPreferences!" , Toast.LENGTH_LONG).show();
//同样,在读取SharedPreferences数据前要实例化出一个SharedPreferences对象
SharedPreferencessharedPreferences= getSharedPreferences("test",
Activity.MODE_PRIVATE);
// 使用getString方法获得value,注意第2个参数是value的默认值
String name =sharedPreferences.getString("name", "");
String habit =sharedPreferences.getString("habit", "");
//使用toast信息提示框显示信息
Toast.makeText(this, "读取数据如下:"+"\n"+"name:" + name + "\n" + "habit:" + habit,
Toast.LENGTH_LONG).show();
provider android:name="com.zj.sqlitedemo.providers.PersonContentProvider"
android:authorities="com.zj.sqlitedemo.providers.PersonContentProvider"
></provider>
private final static String authority ="com.zj.sqlitedemo.providers.PersonContentProvider";
private final static int PERSON_INSERT_CODE=0;
private final static int PERSON_DELETE_CODE=1;
private final static int PERSON_UPDATE_CODE=2;
private final static int PERSON_QUERY_ALL_CODE=3;
private final static UriMatcher uriMatcher;
private PersonSQLiteOpenHelper mOpenHelper;
static
{
uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
//添加一些URI
uriMatcher.addURI(authority, "person/insert", PERSON_INSERT_CODE);
uriMatcher.addURI(authority, "person/delete", PERSON_DELETE_CODE);
uriMatcher.addURI(authority, "person/update", PERSON_UPDATE_CODE);
uriMatcher.addURI(authority, "person/queryAll", PERSON_QUERY_ALL_CODE);
}
public boolean onCreate() 在创建ContentProvider时调用
public Cursor query(Uri, String[], String, String[], String) 用于查询指定Uri的ContentProvider,返回一个Cursor
public Uri insert(Uri, ContentValues) 用于添加数据到指定Uri的ContentProvider中
public int update(Uri, ContentValues, String, String[]) 用于更新指定Uri的ContentProvider中的数据
public int delete(Uri, String, String[]) 用于从指定Uri的ContentProvider中删除数据
public String getType(Uri) 用于返回指定的Uri中的数据的MIME类型
*如果操作的数据属于集合类型,那么MIME类型字符串应该以vnd.android.cursor.dir/开头。
例如:要得到所有person记录的Uri为content://contacts/person,那么返回的MIME类型字符串为”vnd.android.cursor.dir/person”。
*如果要操作的数据属于非集合类型数据,那么MIME类型字符串应该以vnd.android.cursor.item/开头。
例如:要得到id为10的person记录的Uri为content://contacts/person/10,那么返回的MIME类型字符串应为”vnd.android.cursor.item/person”。
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
switch(uriMatcher.match(uri))
{
case PERSON_QUERY_ALL_CODE:
//从表中更新
SQLiteDatabase db= mOpenHelper.getWritableDatabase();
if(db.isOpen())
{
Cursor cursor= db.query("person", projection, selection, selectionArgs,null,null,sortOrder);
return cursor;
}
break;
default:
throw new IllegalArgumentException("URI不匹配"+uri);
}
return null;
}
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
switch(uriMatcher.match(uri))
{
case PERSON_INSERT_CODE:
//添加到表中
SQLiteDatabase db= mOpenHelper.getWritableDatabase();
if(db.isOpen())
{
long id=db.insert("person", null, values);
db.close();
return ContentUris.withAppendedId(uri, id);
}
break;
default:
throw new IllegalArgumentException("URI不匹配");
}
return null;
}
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
switch(uriMatcher.match(uri))
{
case PERSON_DELETE_CODE:
//从表中删除
SQLiteDatabase db= mOpenHelper.getWritableDatabase();
if(db.isOpen())
{
int count=db.delete("person", selection, selectionArgs);
db.close();
return count;
}
break;
default:
throw new IllegalArgumentException("URI不匹配"+uri);
}
return 0;
}
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
switch(uriMatcher.match(uri))
{
case PERSON_UPDATE_CODE:
//从表中更新
SQLiteDatabase db= mOpenHelper.getWritableDatabase();
if(db.isOpen())
{
int count=db.update("person", values, selection, selectionArgs);
db.close();
return count;
}
break;
default:
throw new IllegalArgumentException("URI不匹配"+uri);
}
return 0;
}
public String getType(Uri uri) {
// TODO Auto-generated method stub
switch(uriMatcher.match(uri))
{
case PERSON_QUERY_ALL_CODE:
return "vnd.android.cursor.dir/person";
default:
break;
}
return null;
}
public void testInsert()
{
Uri uri=Uri.parse("content://com.zj.sqlitedemo.providers.PersonContentProvider/person/insert");
ContentResolver resolver= getContext().getContentResolver();
ContentValues values=new ContentValues();
values.put("name", "在吗");
values.put("age", 25);
uri=resolver.insert(uri,values);
Log.i(tag, "uri"+uri);
long id=ContentUris.parseId(uri);
Log.i(tag, "添加到"+id);
}
public void testDelete()
{
Uri uri=Uri.parse("content://com.zj.sqlitedemo.providers.PersonContentProvider/person/delete");
ContentResolver resolver= getContext().getContentResolver();
String where="_id=?";
String []selectionArgs={"21"};
int count=resolver.delete(uri, where, selectionArgs);
Log.i(tag, "删除了行:"+count);
}
public void testUpdate()
{
Uri uri=Uri.parse("content://com.zj.sqlitedemo.providers.PersonContentProvider/person/delete");
ContentResolver resolver= getContext().getContentResolver();
ContentValues values=new ContentValues();
values.put("name", "zj");
int count=resolver.update(uri, values, "_id=?", new String[]{"20"});
Log.i(tag, "更新了"+count);
}
public void testQueryAll()
{
Uri uri=Uri.parse("content://com.zj.sqlitedemo.providers.PersonContentProvider/person/queryAll");
ContentResolver resolver= getContext().getContentResolver();
Cursor cursor=resolver.query(uri, new String[]{"_id","name","age"}, null, null, null);
if(cursor!=null&&cursor.getCount()>0)
{
int id;
String name;
int age;
while(cursor.moveToNext())
{
id=cursor.getInt(0);
name=cursor.getString(1);
age=cursor.getInt(2);
Log.i(tag, "id:"+id+"name:"+name+"age:"+age);
}
cursor.close();
}
}