//實例化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時調用javascript
public Cursor query(Uri, String[], String, String[], String) 用於查詢指定Uri的ContentProvider,返回一個Cursorjava
public Uri insert(Uri, ContentValues) 用於添加數據到指定Uri的ContentProvider中android
public int update(Uri, ContentValues, String, String[]) 用於更新指定Uri的ContentProvider中的數據sql
public int delete(Uri, String, String[]) 用於從指定Uri的ContentProvider中刪除數據markdown
public String getType(Uri) 用於返回指定的Uri中的數據的MIME類型app
*若是操做的數據屬於集合類型,那麼MIME類型字符串應該以vnd.android.cursor.dir/開頭。ssh
例如:要獲得全部person記錄的Uri爲content://contacts/person,那麼返回的MIME類型字符串爲」vnd.android.cursor.dir/person」。ide
*若是要操做的數據屬於非集合類型數據,那麼MIME類型字符串應該以vnd.android.cursor.item/開頭。ui
例如:要獲得id爲10的person記錄的Uri爲content://contacts/person/10,那麼返回的MIME類型字符串應爲」vnd.android.cursor.item/person」。this
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();
}
}