Android数据存储之sharedpreferences与Content Provider
android中對數(shù)據(jù)操作包含有:
file, sqlite3, Preferences, ContectResolver與ContentProvider前三種數(shù)據(jù)操作方式都只是針對本應(yīng)用內(nèi)數(shù)據(jù),程序不能通過這三種方法去操作別的應(yīng)用內(nèi)的數(shù)據(jù)
其中sqlite3已經(jīng)在上一節(jié)中講述了,本節(jié)主要包含sharedpreferences與ContentProvider
sharedpreferences保存數(shù)據(jù)
//實例化SharedPreferences對象(第一步) SharedPreferences mySharedPreferences= getSharedPreferences("test", Activity.MODE_PRIVATE); //實例化SharedPreferences.Editor對象(第二步) SharedPreferences.Editor editor = mySharedPreferences.edit(); //用putString的方法保存數(shù)據(jù) editor.putString("name", "Karl"); editor.putString("habit", "sleep"); //提交當(dāng)前數(shù)據(jù) editor.commit(); //使用toast信息提示框提示成功寫入數(shù)據(jù) Toast.makeText(this, "數(shù)據(jù)成功寫入SharedPreferences!" , Toast.LENGTH_LONG).show();執(zhí)行以上代碼,SharedPreferences將會把這些數(shù)據(jù)保存在test.xml文件中,可以在File Explorer的data/data/相應(yīng)的包名/test.xml 下導(dǎo)出該文件,并查看。
2、使用SharedPreferences讀取數(shù)據(jù)方法如下:
//同樣,在讀取SharedPreferences數(shù)據(jù)前要實例化出一個SharedPreferences對象 SharedPreferencessharedPreferences= getSharedPreferences("test", Activity.MODE_PRIVATE); // 使用getString方法獲得value,注意第2個參數(shù)是value的默認值 String name =sharedPreferences.getString("name", ""); String habit =sharedPreferences.getString("habit", ""); //使用toast信息提示框顯示信息 Toast.makeText(this, "讀取數(shù)據(jù)如下:"+"\n"+"name:" + name + "\n" + "habit:" + habit, Toast.LENGTH_LONG).show();內(nèi)容提供者
在manifest文件中注冊,安卓四大組件都要在其中注冊
provider android:name="com.zj.sqlitedemo.providers.PersonContentProvider"android:authorities="com.zj.sqlitedemo.providers.PersonContentProvider"></provider>設(shè)置訪問路徑,供其他調(diào)用者訪問
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);//添加一些URIuriMatcher.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() 在創(chuàng)建ContentProvider時調(diào)用
public Cursor query(Uri, String[], String, String[], String) 用于查詢指定Uri的ContentProvider,返回一個Cursor
public Uri insert(Uri, ContentValues) 用于添加數(shù)據(jù)到指定Uri的ContentProvider中
public int update(Uri, ContentValues, String, String[]) 用于更新指定Uri的ContentProvider中的數(shù)據(jù)
public int delete(Uri, String, String[]) 用于從指定Uri的ContentProvider中刪除數(shù)據(jù)
public String getType(Uri) 用于返回指定的Uri中的數(shù)據(jù)的MIME類型
*如果操作的數(shù)據(jù)屬于集合類型,那么MIME類型字符串應(yīng)該以vnd.android.cursor.dir/開頭。
例如:要得到所有person記錄的Uri為content://contacts/person,那么返回的MIME類型字符串為”vnd.android.cursor.dir/person”。
*如果要操作的數(shù)據(jù)屬于非集合類型數(shù)據(jù),那么MIME類型字符串應(yīng)該以vnd.android.cursor.item/開頭。
例如:要得到id為10的person記錄的Uri為content://contacts/person/10,那么返回的MIME類型字符串應(yīng)為”vnd.android.cursor.item/person”。
方法實現(xiàn)
查詢方法
public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {// TODO Auto-generated method stubswitch(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 stubswitch(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;}刪除方法實現(xiàn)
public int delete(Uri uri, String selection, String[] selectionArgs) {// TODO Auto-generated method stubswitch(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;}更新方法實現(xiàn)
public int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {// TODO Auto-generated method stubswitch(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 stubswitch(uriMatcher.match(uri)){case PERSON_QUERY_ALL_CODE:return "vnd.android.cursor.dir/person";default:break;}return null;}注意: *如果操作的數(shù)據(jù)屬于集合類型,那么MIME類型字符串應(yīng)該以vnd.android.cursor.dir/開頭。
例如:要得到所有person記錄的Uri為content://contacts/person,那么返回的MIME類型字符串為”vnd.android.cursor.dir/person”。
*如果要操作的數(shù)據(jù)屬于非集合類型數(shù)據(jù),那么MIME類型字符串應(yīng)該以vnd.android.cursor.item/開頭。
例如:要得到id為10的person記錄的Uri為content://contacts/person/10,那么返回的MIME類型字符串應(yīng)為”vnd.android.cursor.item/person”。
例如此處就用了vnd.android.cursor.dir開頭
ContentResolver解析內(nèi)容提供者提供的數(shù)據(jù),當(dāng)外部應(yīng)用需要對ContentProvider中的數(shù)據(jù)進行添加、刪除、修改和查詢操作時,可以使用ContentResolver類來完成,要獲取ContentResolver對象,可以使用Context提供的getContentResolver()方法
插入實現(xiàn)
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);}刪除實現(xiàn)
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);}更新實現(xiàn)
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);}查詢實現(xiàn)
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();}}內(nèi)容提供者與sharedpreferences完成
總結(jié)
以上是生活随笔為你收集整理的Android数据存储之sharedpreferences与Content Provider的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows下同时安装python2与
- 下一篇: 虚拟机磁盘扩容流程