一、什麼是ContentProvider(內容提供者)呢?ContentProvider屬於安卓四大組件之一。ContentProvider將訪問的方式統一,如數據庫、文件等。都可以通過ContentProvider來訪問,不必針對不同的數據類型採取不同的訪問方式。這樣我們就可以控制那些內容,其他程序可以訪問,那些內容其他程序不可以訪問,即限制了其他程序的訪問權限,保證我們的數據安全性。ContentProvider的數據更改可被監聽,如:監聽信息的變化,通話記錄的變化等。我們就可以採取相應的一些操作。
二、怎麼創建ContentProvider?分爲2步。
1、創建一個類繼承ContentProvider(抽象類),重寫裏面的六個抽象方法
2、在清單文件(AndroidManifest.xml)中註冊,在application節點下創建一個provider節點。必須要指定name,authorities屬性
name:我們創建類的名稱,要寫全類名
authorities:一個唯一標示,標示我們這個內容提供者只有一個,一般情況下我們就用域名錶示,如:com.51cto.blog.6272409
- static{
- UriMatcher.addURI("com.51cto.providers.personprovider", "person", 1);
- //#在java中代表數字
- UriMatcher.addURI("com.51cto.providers.personprovider", "person/#", 2);
- }
- @Override
- public boolean onCreate() { //當PersonProvider實例被創建之後,系統調用,只調用一次
- //數據庫的初始化操作,一般情況下把數據庫的初始放在這個方法裏面
- SQLiteOpenHelper msoh = new MySQLiteOpenHelper(this.getContext());
- System.out.println("onCreate執行了");
- return true;
- }
- @Override
- public Cursor query(Uri uri, String[] projection, String selection,
- String[] selectionArgs, String sortOrder) {//可以供外部的應用,查詢內容提供者的數據
- SQLiteDatabase db = msoh.getReadableDatabase();
- //判斷Uri 通過一個類 UriMatcher
- switch(MATCHER.match(uri)){
- case 1:
- return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
- case 2:
- //通過ContentUris得到id,可以截取uri中的id
- long id = ContentUris.parseId(uri);
- String where = "id="+id;
- //判斷外面是否有條件傳進來
- if(selection!=null && !"".equals(selection.trim())){
- where += " and "+selection;
- }
- return db.query("person", projection, where, selectionArgs, null, null , sortOrder);
- default:
- throw new IllegalArgumentException("不是這個"+uri);
- }
- }
- @Override
- public String getType(Uri uri) {//返回要操作的內容類型
- switch(MATCHER.match(uri)){
- case 1:
- return "vnd.android.cursor.dir/person";//多條數據
- case 2:
- return "vnd.android.cursor.item/person";//單條數據
- default:
- throw new IllegalArgumentException("不是這個"+uri);
- }
- }
- @Override
- public Uri insert(Uri uri, ContentValues values) {
- //得到數據庫
- SQLiteDatabase db = msoh.getWritableDatabase();
- //判斷Uri 通過一個類 UriMatcher
- switch(MATCHER.match(uri)){
- case 1:
- long rowId = db.insert("person", "id", values);//主鍵值
- //工具類 ContentUris.withAppendedId(contentUri, id)將以個id添加到uri中
- Uri insertUri = ContentUris.withAppendedId(uri, rowId);
- this.getContext().getContentResolver().notifyChange(uri, null);
- return insertUri;
- default:
- throw new IllegalArgumentException("不是這個"+uri);
- }
- }
- @Override
- public int delete(Uri uri, String selection, String[] selectionArgs) {//放回影響到的記錄數
- //得到數據庫
- SQLiteDatabase db = msoh.getWritableDatabase();
- int count = 0;
- //判斷Uri 通過一個類 UriMatcher
- switch(MATCHER.match(uri)){
- case 1:
- count = db.delete("person", selection, selectionArgs);
- this.getContext().getContentResolver().notifyChange(uri, null);
- //工具類 ContentUris.withAppendedId(contentUri, id)
- break;
- case 2:
- //通過ContentUris得到id
- long id = ContentUris.parseId(uri);
- String where = "id="+id;
- //判斷外面是否有條件傳進來
- if(selection!=null && !"".equals(selection.trim())){
- where += " and "+selection;
- }
- count = db.delete("person", where, selectionArgs);
- break;
- default:
- throw new IllegalArgumentException("不是這個"+uri);
- }
- return count;
- }
- @Override
- public int update(Uri uri, ContentValues values, String selection,
- String[] selectionArgs) {
- SQLiteDatabase db = msoh.getWritableDatabase();
- int count = 0;
- //判斷Uri 通過一個類 UriMatcher,之前可以調用UriMatcher.addURI();添加一個
- switch(MATCHER.match(uri)){
- case 1:
- count = db.update("person", values, selection, selectionArgs);
- //工具類 ContentUris.withAppendedId(contentUri, id)
- break;
- case 2:
- //通過ContentUris得到id
- long id = ContentUris.parseId(uri);
- String where = "id="+id;
- //判斷外面是否有條件傳進來
- if(selection!=null && !"".equals(selection.trim())){
- where += " and "+selection;
- }
- count = db.update("person", values, where, selectionArgs);
- break;
- default:
- throw new IllegalArgumentException("不是這個"+uri);
- }
- return count;
- }