Android 資料存放區——content providers

來源:互聯網
上載者:User

1.content providers資料共用原理:
當一個應用需要公開共用它的資料時,可以通過建立自己的content providers然後通過Manifest把content providers註冊到android系統,然後其他應用通過contentresolver訪問content providers的uri來操作指定的content providers,實際上,contentresolver就是相當於content providers的一個代理,其它應用程式通過contentresolver訪問共用的content providers。
content providers是單例對象,既它只建立一次,後面對它的訪問都是同一個執行個體
2.建立content providers,實現抽象類別ContentProvider
//實現建立的時候介面
public abstract boolean onCreate();
當content providers第一次被訪問,執行個體化時候它會被調用,以後的訪問oncreate方法將不再被調用。
//實現查詢的時候介面
public abstract Cursor query(Uri uri, String[] projection,
            String selection, String[] selectionArgs, String sortOrder);
uri表示資料content providers的位置路徑,也可以直接使用id定位到一條記錄上去,projection表示查詢指定的列名數組,selection表示查詢條件,用"and"分割多個查詢條件,當查詢條件中存在查詢參數"?"時,需要使用selectionArgs添加查詢參數,sortOrder用來指定查詢順序
//實現擷取資料類型介面
public abstract String getType(Uri uri);
meta資料,擷取資料類型
//實現插入資料介面
public abstract Uri insert(Uri uri, ContentValues values);
values需要插入的內容,uri指定content providers
//實現刪除資料介面
public abstract int delete(Uri uri, String selection, String[] selectionArgs);

//實現更新資料介面
public abstract int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs);
3.使用ContentResolver訪問content providers
//擷取ContentResolver對象
ContentResolver contentResolver = this.getInstrumentation().getContext().getContentResolver();
//插入資料
                ContentValues values = new ContentValues();
                values.put(NotePad.Notes.TITLE, "title1");
                values.put(NotePad.Notes.NOTE, "NOTENOTE1");
                contentResolver.insert(NotePad.Notes.CONTENT_URI, values);
//刪除資料
                ContentResolver contentResolver = this.getInstrumentation().getContext().getContentResolver();
                //指定id刪除
                 contentResolver.delete(ContentUris.withAppendedId(NotePad.Notes.CONTENT_URI,
                 2), null, null);
                 //使用where條件刪除
                 contentResolver.delete(NotePad.Notes.CONTENT_URI,
                 NotePad.Notes.TITLE+"=/"title1/"", null);
                 //使用where條件參數刪除
                contentResolver.delete(NotePad.Notes.CONTENT_URI, NotePad.Notes.TITLE + "=?", new String[] { "title2" });
//修改資料
                ContentResolver contentResolver = this.getInstrumentation().getContext().getContentResolver();
                ContentValues values = new ContentValues();
                values.put(NotePad.Notes.TITLE, "title1");
                values.put(NotePad.Notes.NOTE, "NOTENOTE2");

                contentResolver.update(NotePad.Notes.CONTENT_URI, values, NotePad.Notes.TITLE + "=?", new String[] { "title1" });
//查詢資料
                ContentResolver contentResolver = this.getInstrumentation().getContext().getContentResolver();
                String columns[] = new String[] { NotePad.Notes._ID, NotePad.Notes.TITLE, NotePad.Notes.NOTE, NotePad.Notes.CREATEDDATE, NotePad.Notes.MODIFIEDDATE };
                Cursor cur = contentResolver.query( NotePad.Notes.CONTENT_URI, columns, NotePad.Notes.TITLE + "=?",  new String[] { "title1" }, NotePad.Notes._ID);
                if (cur.moveToFirst()) {
                        String id = null;
                        String title = null;
                        String note = null;
                        do {
                                id = cur.getString(cur.getColumnIndex(NotePad.Notes._ID));
                                title = cur.getString(cur.getColumnIndex(NotePad.Notes.TITLE));
                                note = cur.getString(cur.getColumnIndex(NotePad.Notes.NOTE));
                                System.out.println("Record:"+id+" "+title+" "+note+" ");
                        } while (cur.moveToNext());
                }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.