<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 78, comments - 34, trackbacks - 0, articles - 1
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

           早上我們簡要的對SQLite進行回顧,然后將SQLite的事務(wù)管理和SQLiteDataBase提供的InsertUpdateDeleteQuery方法進行了簡單的講解。

     

           今日的重點內(nèi)容是ContentProvider(內(nèi)容提供者)和網(wǎng)絡(luò)存儲,我將對這兩大內(nèi)容進行總結(jié)。關(guān)于SQLiteDataBase提供的便捷方法,它們會在ContentProvider被使用。SQLite的事務(wù)管理比較簡單,昨天有簡要介紹,今日就不做總結(jié)了。

     

           ContentProvider和網(wǎng)絡(luò)存儲將分為兩篇日志,本篇總結(jié)ContentProvider

     

    一、ContentProvider簡介

           當(dāng)應(yīng)用繼承ContentProvider類,并重寫該類用于提供數(shù)據(jù)和存儲數(shù)據(jù)的方法,就可以向其他應(yīng)用共享其數(shù)據(jù)。雖然使用其他方法也可以對外共享數(shù)據(jù),但數(shù)據(jù)訪問方式會因數(shù)據(jù)存儲的方式而不同,如:采用文件方式對外共享數(shù)據(jù),需要進行文件操作讀寫數(shù)據(jù);采用sharedpreferences共享數(shù)據(jù),需要使用sharedpreferences API讀寫數(shù)據(jù)。而使用ContentProvider共享數(shù)據(jù)的好處是統(tǒng)一了數(shù)據(jù)訪問方式。

     

    二、Uri類簡介

           Uri代表了要操作的數(shù)據(jù),Uri主要包含了兩部分信息:1.需要操作的ContentProvider2.ContentProvider中的什么數(shù)據(jù)進行操作,一個Uri由以下幾部分組成:


           1.schemeContentProvider(內(nèi)容提供者)的scheme已經(jīng)由Android所規(guī)定為:content://

           2.主機名(或Authority):用于唯一標(biāo)識這個ContentProvider,外部調(diào)用者可以根據(jù)這個標(biāo)識來找到它。

           3.路徑(path):可以用來表示我們要操作的數(shù)據(jù),路徑的構(gòu)建應(yīng)根據(jù)業(yè)務(wù)而定,如下:

    ·         要操作contact表中id10的記錄,可以構(gòu)建這樣的路徑:/contact/10

    ·         要操作contact表中id10的記錄的name字段, contact/10/name

    ·         要操作contact表中的所有記錄,可以構(gòu)建這樣的路徑:/contact

     

           要操作的數(shù)據(jù)不一定來自數(shù)據(jù)庫,也可以是文件等他存儲方式,如下:

    要操作xml文件中contact節(jié)點下的name節(jié)點,可以構(gòu)建這樣的路徑:/contact/name

    如果要把一個字符串轉(zhuǎn)換成Uri,可以使用Uri類中的parse()方法,如下:

    Uri uri = Uri.parse("content://com.changcheng.provider.contactprovider/contact")

     

    三、UriMatcherContentUristContentResolver簡介

           因為Uri代表了要操作的數(shù)據(jù),所以我們很經(jīng)常需要解析Uri,并從Uri中獲取數(shù)據(jù)。Android系統(tǒng)提供了兩個用于操作Uri的工具類,分別為UriMatcherContentUris 。掌握它們的使用,會便于我們的開發(fā)工作。

     

           UriMatcher用于匹配Uri,它的用法如下:

           1.首先把你需要匹配Uri路徑全部給注冊上,如下:

           //常量UriMatcher.NO_MATCH表示不匹配任何路徑的返回碼(-1)

           UriMatcher  uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

           //如果match()方法匹配content://com.changcheng.sqlite.provider.contactprovider/contact路徑,返回匹配碼為1

           uriMatcher.addURI(“com.changcheng.sqlite.provider.contactprovider”, “contact”, 1);//添加需要匹配uri,如果匹配就會返回匹配碼

           //如果match()方法匹配   content://com.changcheng.sqlite.provider.contactprovider/contact/230路徑,返回匹配碼為2

           uriMatcher.addURI(“com.changcheng.sqlite.provider.contactprovider”, “contact/#”, 2);//#號為通配符

          

           2.注冊完需要匹配的Uri后,就可以使用uriMatcher.match(uri)方法對輸入的Uri進行匹配,如果匹配就返回匹配碼,匹配碼是調(diào)用addURI()方法傳入的第三個參數(shù),假設(shè)匹配content://com.changcheng.sqlite.provider.contactprovider/contact路徑,返回的匹配碼為1

     

           ContentUris用于獲取Uri路徑后面的ID部分,它有兩個比較實用的方法:

    ·         withAppendedId(uri, id)用于為路徑加上ID部分

    ·         parseId(uri)方法用于從路徑中獲取ID部分

     

           ContentResolver當(dāng)外部應(yīng)用需要對ContentProvider中的數(shù)據(jù)進行添加、刪除、修改和查詢操作時,可以使用ContentResolver 類來完成,要獲取ContentResolver 對象,可以使用Activity提供的getContentResolver()方法。 ContentResolver使用insertdeleteupdatequery方法,來操作數(shù)據(jù)。

     

    四、ContentProvider示例程序

           我們?yōu)樽蛱斓?span lang="EN-US" xml:lang="EN-US">SQLite示例程序添加一個ContentProvider,供其他應(yīng)用來訪問我們的數(shù)據(jù)。

     

           1.SQLite示例程序添加ContentProvider

    package com.changcheng.sqlite.provider;

     

    import com.changcheng.sqlite.MyOpenHelper;

    import android.content.ContentProvider;

    import android.content.ContentUris;

    import android.content.ContentValues;

    import android.content.UriMatcher;

    import android.database.Cursor;

    import android.database.sqlite.SQLiteDatabase;

    import android.net.Uri;

     

    public class ContactContentProvider extends ContentProvider {

     

             // 通過UriMatcher匹配外部請求

             private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

             // 通過openHelper進行數(shù)據(jù)庫讀寫

             private MyOpenHelper openHelper;

             // 匹配狀態(tài)常量

             private static final int CONTACT_LIST = 1;

             private static final int CONTACT = 2;

             // 表名

             private static final String tableName = "contacts";

             // 添加Uri

             static {

                       uriMatcher.addURI("com.changcheng.sqlite.provider", "contact",

                                         CONTACT_LIST);

                       uriMatcher.addURI("com.changcheng.sqlite.provider", "contact/#",

                                         CONTACT);

             }

     

             @Override

             public int delete(Uri uri, String selection, String[] selectionArgs) {

                       SQLiteDatabase db = this.openHelper.getWritableDatabase();

                      int result;

                       switch (uriMatcher.match(uri)) {

                       case CONTACT_LIST:

                                result = db.delete(tableName, selection, selectionArgs);

                                break;

                       case CONTACT:

                                long id = ContentUris.parseId(uri);

                                String where = "_id=" + id;

                                if (selection != null && !"".equals(selection)) {

                                         where = where + " and " + selection;

                                }

                                result = db.delete(tableName, where, selectionArgs);

                                break;

                       default:

                                throw new IllegalArgumentException("Uri IllegalArgument:" + uri);

                       }

                       return result;

             }

     

             @Override

             public String getType(Uri uri) {

                       switch (uriMatcher.match(uri)) {

                       case CONTACT_LIST:// 集合類型必須在前面加上vnd.android.cursor.dir/

                                return "vnd.android.cursor.dir/contactlist";

                       case CONTACT:// 非集合類型必須在前面加上vnd.android.cursor.item/

                                return "vnd.android.cursor.item/contact";

                       default:

                                throw new IllegalArgumentException("Uri IllegalArgument:" + uri);

                       }

             }

     

             @Override

             public Uri insert(Uri uri, ContentValues values) {

                       SQLiteDatabase db = this.openHelper.getWritableDatabase();

                       long id;

                       switch (uriMatcher.match(uri)) {

                       case CONTACT_LIST:

                                // 因為后臺需要生成SQL語句,當(dāng)valuesnull時,必須提第二個參數(shù)。生成的SQL語句才不會出錯!

                                id = db.insert(tableName, "_id", values);

                                return ContentUris.withAppendedId(uri, id);

                       case CONTACT:

                                id = db.insert(tableName, "_id", values);

                                String uriPath = uri.toString();

                                String path = uriPath.substring(0, uriPath.lastIndexOf("/")) + id;

                                return Uri.parse(path);

                       default:

                                throw new IllegalArgumentException("Uri IllegalArgument:" + uri);

                       }

             }

     

             @Override

             public boolean onCreate() {

                       this.openHelper = new MyOpenHelper(this.getContext());

                       return true;

             }

     

             @Override

             public Cursor query(Uri uri, String[] projection, String selection,

                                String[] selectionArgs, String sortOrder) {

                       SQLiteDatabase db = this.openHelper.getWritableDatabase();

                       switch (uriMatcher.match(uri)) {

                       case CONTACT_LIST:

                                return db.query(tableName, projection, selection, selectionArgs,

                                                   null, null, sortOrder);

                       case CONTACT:

                                long id = ContentUris.parseId(uri);

                                String where = "_id=" + id;

                                if (selection != null && !"".equals(selection)) {

                                         where = where + " and " + selection;

                                }

                                return db.query(tableName, projection, where, selectionArgs, null,

                                                   null, sortOrder);

                       default:

                                throw new IllegalArgumentException("Uri IllegalArgument:" + uri);

                       }

             }

     

             @Override

             public int update(Uri uri, ContentValues values, String selection,

                                String[] selectionArgs) {

                       SQLiteDatabase db = this.openHelper.getWritableDatabase();

                       int result;

                       switch (uriMatcher.match(uri)) {

                       case CONTACT_LIST:

                                result = db.update(selection, values, selection, selectionArgs);

                                break;

                       case CONTACT:

                                long id = ContentUris.parseId(uri);

                                String where = "_id=" + id;

                                if (selection != null && !"".equals(selection)) {

                                         where = where + " and " + selection;

                                }

                                result = db.update(tableName, values, where, selectionArgs);

                                break;

                       default:

                                throw new IllegalArgumentException("Uri IllegalArgument:" + uri);

                       }

                       return result;

             }

     

    }

     

     

           2.添加ContentProvider配置

    <provider android:name=".provider.ContactContentProvider" android:authorities="com.changcheng.sqlite.provider.contactprovider"/>

     

           3.測試SQLite示例程序的ContentProvider

           ContentProvider即然是提供給其他應(yīng)用訪問本應(yīng)用數(shù)據(jù)的,所以我們需要另創(chuàng)建一個Android應(yīng)用,來測試SQLite示例程序的ContentProvider。我在此只列出query的測試方法testQuery

    public void testQuery() throws Throwable {

             ContentResolver contentResolver = this.getContext()

                                .getContentResolver();

             Uri uri = Uri

                                .parse("content://com.changcheng.sqlite.provider/contact");

             Cursor cursor = contentResolver.query(uri, new String[] { "_id",

                                "name", "phone" }, null, null, "_id desc");

             while (cursor.moveToNext()) {

                       Log.i(TAG, "_id=" + cursor.getInt(0) + ",name="

                                         + cursor.getString(1) + ",phone=" + cursor.getString(2));

             }

    }

     

           下一篇:Android數(shù)據(jù)存儲之網(wǎng)絡(luò)

     


    評論

    # re: 2010-03-02 傳智播客—Android(四)數(shù)據(jù)存儲之四ContentProvider  回復(fù)  更多評論   

    2010-07-20 11:26 by Cangol
    你好!我有些疑問?
    在provider 類中我們寫注冊uri
    uriMatcher.addURI("com.changcheng.sqlite.provider", "contact",
    CONTACT_LIST);
    標(biāo)示com.changcheng.sqlite.provider這個是在哪定義的,若是自定義的,將會在哪用到。
    以下在配置文件中的Authority 是否應(yīng)該與Uri中的一致?

    <provider android:name=".provider.ContactContentProvider" android:authorities="com.changcheng.sqlite.provider.contactprovider"/>

    # re: 2010-03-02 傳智播客—Android(四)數(shù)據(jù)存儲之四ContentProvider  回復(fù)  更多評論   

    2010-07-20 13:11 by 長城
    @Cangol
    請仔細看文章中的例子代碼:
    // 添加Uri
    static {
    uriMatcher.addURI("com.changcheng.sqlite.provider", "contact",
    CONTACT_LIST);
    uriMatcher.addURI("com.changcheng.sqlite.provider", "contact/#",
    CONTACT);
    }

    # re: 2010-03-02 傳智播客—Android(四)數(shù)據(jù)存儲之四ContentProvider  回復(fù)  更多評論   

    2010-07-20 17:16 by Cangol
    @長城
    我還是不解 配置文件中的這個android:authorities做什么用?
    我寫的還是說failed not find provider info com.changcheng.sqlite.provider
    向您請教?

    # re: 2010-03-02 傳智播客—Android(四)數(shù)據(jù)存儲之四ContentProvider  回復(fù)  更多評論   

    2010-08-16 15:52 by 漂泊
    @Cangol
    com.changcheng.sqlite.provider就是你應(yīng)用的包名、、。

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 永久免费观看的毛片的网站| 中文字幕亚洲色图| 成人奭片免费观看| a级毛片毛片免费观看久潮喷| 精品亚洲成在人线AV无码| 国产亚洲精品精华液| 国产真实伦在线视频免费观看| 在线观看免费视频资源| WWW免费视频在线观看播放| 国产成人人综合亚洲欧美丁香花| 亚洲色图古典武侠| 亚洲AV无码久久精品成人| 亚洲国产精品尤物YW在线观看| 黄瓜视频高清在线看免费下载| 污视频在线免费观看| a级毛片高清免费视频就| jizz免费在线观看| 美国免费高清一级毛片| 亚洲av日韩aⅴ无码色老头| 色在线亚洲视频www| 亚洲国产午夜精品理论片| 亚洲午夜视频在线观看| 久久亚洲国产中v天仙www| 亚洲精品自产拍在线观看| 国产精品V亚洲精品V日韩精品 | 在线亚洲精品福利网址导航| 大学生一级特黄的免费大片视频| 四虎在线视频免费观看视频| 最好看的中文字幕2019免费| 亚洲欧洲免费视频| 久久永久免费人妻精品下载| 无码人妻精品中文字幕免费| 先锋影音资源片午夜在线观看视频免费播放| 成人免费777777被爆出| aa级女人大片喷水视频免费| 99在线免费视频| 免费毛片在线看不用播放器| 久久亚洲免费视频| 亚洲精品在线免费观看| 国产成人免费网站| 在线精品免费视频无码的|