ContentProvider

1.什麼是ContentProvider
 數據庫在Android當中是私有的,當然這些數據包括文件數據和數據庫數據以及一些其他類型的數據。
 不能將數據庫設爲
WORLD_READABLE,每個數據庫都只能創建它的包訪問,
 這意味着只有由創建數據庫的進程可訪問它。如果需要在進程間傳遞數據,
 則可以使用
AIDL/Binder或創建一個ContentProvider,但是不能跨越進程/包邊界直接來使用數據庫。
 一個
Content Provider類實現了一組標準的方法接口,從而能夠讓其他的應用保存或讀取此Content Provider的各種數據類型。
 也就是說,一個程序可以通過實現一個
Content Provider的抽象接口將自己的數據暴露出去。
 外界根本看不到,也不用看到這個應用暴露的數據在應用當中是如何存儲的,或者是用數據庫存儲還是用文件存儲,還是通過網上獲得,這些一切都不重要,
 重要的是外界可以通過這一套標準及統一的接口和程序裏的數據打交道,可以讀取程序的數據,也可以刪除程序的數據,
 當然,中間也會涉及一些權限的問題。下邊列舉一些較常見的接口,這些接口如下所示

·  query(Uri uri, String[] projection, String selection, String[] selectionArgs,String sortOrder):通過Uri進行查詢,返回一個Cursor。
·  insert(Uri url, ContentValues values):將一組數據插入到Uri 指定的地方。
·  update(Uri uri, ContentValues values, String where, String[] selectionArgs):更新Uri指定位置的數據。
·  delete(Uri url, String where, String[] selectionArgs):刪除指定Uri並且符合一定條件的數據。
2.什麼是ContentResolver
外界的程序通過ContentResolver接口可以訪問ContentProvider提供的數據,在Activity當中通過getContentResolver()可以得到當前應用的 ContentResolver實例。
 ContentResolver提供的接口和ContentProvider中需要實現的接口對應,主要有以下幾個。

·  query(Uri uri, String[] projection, String selection, String[] selectionArgs,String sortOrder):通過Uri進行查詢,返回一個Cursor。
·  
insert(Uri url, ContentValues values):將一組數據插入到Uri 指定的地方。
·  update(Uri uri, ContentValues values, String where, String[] selectionArgs):更新Uri指定位置的數據。
·  delete(Uri url, String where, String[] selectionArgs):刪除指定Uri並且符合一定條件的數據。
3.ContentProvider和ContentResolver中用到的Uri
在ContentProvider和 ContentResolver當中用到了Uri的形式通常有兩種,一種是指定全部數據,另一種是指定某個ID的數據。
我們看下面的例子。

·  content://contacts/people/  這個Uri指定的就是全部的聯繫人數據。
·  content://contacts/people/1 這個Uri指定的是ID爲1的聯繫人的數據。 
在上邊兩個類中用到的Uri一般由3部分組成。
·  第一部分是方案:"content://" 這部分永遠不變
·  第二部分是授權:"contacts"
·  第二部分是路徑:"people/","people/1"(如果沒有指定ID,那麼表示返回全部)。
由於URI通常比較長,而且有時候容易出錯,且難以理解。所以,在Android當中定義了一些輔助類,並且定義了一些常量來代替這些長字符串的使用,例如下邊的代碼:
·  Contacts.People.CONTENT_URI (聯繫人的URI)。
在我們的實例MyProvider中是如下定義的:
public static final String AUTHORITY="com.teleca.PeopleProvider";
public static final String PATH_SINGLE="people/#";
public static final String PATH_MULTIPLE="people";
public static final Uri content_URI=Uri.parse("content://"+AUTHORITY+"/"+PATH_MULTIPLE);

實例1:
文件MyProvider.java
package com.teleca.provider;
import java.util.HashMap;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
public class MyProvider extends ContentProvider {
 public static final String MIME_DIR_PREFIX="vnd.android.cursor.dir";
 public static final String MIME_ITEM_PREFIX="vnd.android.cursor.item";
 public static final String MIME_ITEM="vnd.msi.people";
 public static final String MIME_TYPE_SINGLE=MIME_ITEM_PREFIX+"/"+MIME_ITEM;
 public static final String MIME_TYPE_MULTIPLE=MIME_DIR_PREFIX+"/"+MIME_ITEM;
 public static final String AUTHORITY="com.teleca.PeopleProvider";
 public static final String PATH_SINGLE="people/#";
 public static final String PATH_MULTIPLE="people";
 public static final Uri content_URI=Uri.parse("content://"+AUTHORITY+"/"+PATH_MULTIPLE);
 public static final String DEFAULT_SORT_ORDER="name DESC";
 public static final String _ID="_id";
 public static final String NAME="name";
 public static final String PHONE="phone";
 public static final String AGE="age";
 public static final int PEOPLE=1;
 public static final int PEOPLES=2;
 private static UriMatcher URI_MATCHER;
 private static HashMap<String,String> PROJECTION_MAP;
 public static String DB_NAME="peopledb";
 public static String DB_TABLE_NAME="people";
 SQLiteDatabase db;
 DBOpenHelper dbOpenHelper;
 static 
 {
  URI_MATCHER=new UriMatcher(UriMatcher.NO_MATCH);
  URI_MATCHER.addURI(AUTHORITY, PATH_MULTIPLE, PEOPLES);
  URI_MATCHER.addURI(AUTHORITY, PATH_SINGLE, PEOPLE);
  PROJECTION_MAP=new HashMap<String,String>();
  PROJECTION_MAP.put(_ID, "_id");
  PROJECTION_MAP.put(NAME, "name");
  PROJECTION_MAP.put(PHONE, "phone");
  PROJECTION_MAP.put(AGE, "age");
 }
 @Override
 public int delete(Uri uri, String selection, String[] selectionArgs) {
  // TODO Auto-generated method stub
  int count=0;
  switch(URI_MATCHER.match(uri))
  {
  case PEOPLES:
   count=db.delete(DB_TABLE_NAME,  selection, selectionArgs);
   break;
  case PEOPLE:
   String segment =uri.getPathSegments().get(1);
   String where="";
   if(!TextUtils.isEmpty(selection))
   {
    where=" AND ("+selection+")";
   }
   count=db.delete(DB_TABLE_NAME, "_id="+segment+where, selectionArgs);
   break;
  default:
   throw new IllegalArgumentException("Unkonw URI"+uri);
  }
  getContext().getContentResolver().notifyChange(uri, null);//@2
  return count;
 }
 @Override
 public String getType(Uri uri) {
  // TODO Auto-generated method stub
  switch(URI_MATCHER.match(uri))
  {
  case PEOPLES:
   return MIME_TYPE_MULTIPLE;
  case PEOPLE:
   return MIME_TYPE_SINGLE;
  default:
   throw new IllegalArgumentException("Unkown URI "+uri);
  }
 }
 @Override
 public Uri insert(Uri uri, ContentValues values) {
  // TODO Auto-generated method stub
  long rowId=0L;
  if(URI_MATCHER.match(uri)!=PEOPLES)
  {
   throw new IllegalArgumentException("Unkown URI"+uri);
  }
  rowId=db.insert(DB_TABLE_NAME, null, values);
  if(rowId>0)
  {
   Uri result=ContentUris.withAppendedId(content_URI, rowId);
   getContext().getContentResolver().notifyChange(result, null);//@2
   return result;
  }
  else
   throw new SQLException("Failed to insert row into "+uri);
 }
 @Override
 public boolean onCreate() {
  // TODO Auto-generated method stub
  dbOpenHelper=new DBOpenHelper(this.getContext(),DB_NAME,1);
  db=dbOpenHelper.getWritableDatabase();
  return false;
 }
 @Override
 public Cursor query(Uri uri, String[] projection, String selection,
   String[] selectionArgs, String sortOrder) {
  // TODO Auto-generated method stub
  SQLiteQueryBuilder queryBuilder=new SQLiteQueryBuilder();
  queryBuilder.setTables(DBInfo.DB_TABLE_NAME);
  queryBuilder.setProjectionMap(PROJECTION_MAP);
  switch(URI_MATCHER.match(uri))
  {
  
case PEOPLES:
   break;
  
case  PEOPLE:
   queryBuilder.appendWhere("_id="+uri.getPathSegments().get(1));
   break;
  
default:
   throw new IllegalArgumentException("Unkonw URI"+uri);
  }
  String orderBy=null;
  
if(TextUtils.isEmpty(sortOrder))
  {
   orderBy=DEFAULT_SORT_ORDER;
  }
  
else
   orderBy=sortOrder;
  Cursor c=queryBuilder.query(db, projection, selection, selectionArgs, null, null, orderBy);
  c.setNotificationUri(getContext().getContentResolver(), uri);//@1
  return c;
 }
 @Override
 public int update(Uri uri, ContentValues values, String selection,
   String[] selectionArgs) {
  // TODO Auto-generated method stub
  int count=0;
  switch(URI_MATCHER.match(uri))
  {
  case PEOPLES:
   count=db.update(DB_TABLE_NAME, values, selection, selectionArgs);
   break;
  case PEOPLE:
   String segment =uri.getPathSegments().get(1);
   String where="";
   if(!TextUtils.isEmpty(selection))
   {
    where=" AND ("+selection+")";
   }
   count=db.update(DB_TABLE_NAME, values, "_id="+segment+where, selectionArgs);
   break;
  default:
   throw new IllegalArgumentException("Unkonw URI"+uri);
  }
  getContext().getContentResolver().notifyChange(uri, null);//@2
  return count;
 }
}
class DBOpenHelper extends SQLiteOpenHelper
{
 private static final String DB_CREATE="CREATE TABLE "
  +DBInfo.DB_TABLE_NAME
  +" (_id INTEGER PRIMARY KEY,name TEXT UNIQUE NOT NULL,"
  +"phone TEXT,age INTEGER);";
 final static String tag="hubin";
 public DBOpenHelper(Context context,String dbName,int version)
 {
  super(context,dbName,null,version);
 }
 public void onCreate(SQLiteDatabase db)
 {
  try{
   db.execSQL(DB_CREATE);
  }
  catch(SQLException e )
  {
   Log.e(tag,"",e);
  }
 }
 public void onOpen(SQLiteDatabase db)
 {
  super.onOpen(db);
 }
 public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
 {
  db.execSQL("DROP TABLE IF EXISTS "+DBInfo.DB_TABLE_NAME);
  this.onCreate(db);
 }
}
class DBInfo
{
 public static String DB_NAME="peopledb";
 public static String DB_TABLE_NAME="people"; 
}
注意1:c.setNotificationUri(getContext().getContentResolver(), uri);
這裏是把Cursor C添加到ContentResolver的監督對象組中去。
一旦有與uri相關的變化,ContentResolver就回通知Cursor C.
可能Cursor有個私有的內部類ContentObserver的實現。ContentResolver是通過該類來通知Cursor的。
public abstract void  setNotificationUri  (ContentResolver  cr, Uri  uri)
Register to watch a content URI for changes. This can be the URI of a specific data row (for example, "content://my_provider_type/23"), 
or a a generic URI for a content type.

Parameters
cr  The content resolver from the caller's context. The listener attached to this resolver will be notified.
uri  The content URI to watch. 
注意2: getContext().getContentResolver().notifyChange(uri, null)
通知數據發生了變化。
  public void  notifyChange  (Uri  uri, ContentObserver  observer)
Notify registered observers that a row was updated. To register, call registerContentObserver(). By default, CursorAdapter objects will get this notification.
Parameters
observer  The observer that originated the change, may be null 
這裏爲null的意思可能就是調用在ContentResolver中註冊的ContentObserver,反之則是調用參數指定的
文件People.java
package com.teleca.provider;
public class People {
 public long id;
 public String name;
 public String phone;
 public int age;
}

文件
Hello.java
package com.teleca.provider;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Hello extends Activity {
    
/** Called when the activity is first created. */
 final static String tag="hubin";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
  Button button = (Button) findViewById(R.id.Button01);
  OnClickListener listener = new OnClickListener() {
   @Override
   public void onClick(View v) {
    cmd = CMD_ADD;
    doAction();

   }
  };
  button.setOnClickListener(listener);
  Button button2 = (Button) findViewById(R.id.Button02);
  OnClickListener listener2 = new OnClickListener() {
   @Override
   public void onClick(View v) {
    cmd = CMD_UPDATE;
    doAction();

   }
  };
  button2.setOnClickListener(listener2);
  Button button3 = (Button) findViewById(R.id.Button03);
  OnClickListener listener3 = new OnClickListener() {
   @Override
   public void onClick(View v) {
    cmd = CMD_QUERY;
    doAction();

   }
  };
  button3.setOnClickListener(listener3);
  Button button4 = (Button) findViewById(R.id.Button04);
  OnClickListener listener4 = new OnClickListener() {
   @Override
   public void onClick(View v) {
    cmd = CMD_QUERY_ALL;
    doAction();

   }
  };
  button4.setOnClickListener(listener4);
  Button button5 = (Button) findViewById(R.id.Button05);
  OnClickListener listener5 = new OnClickListener() {
   @Override
   public void onClick(View v) {
    cmd = CMD_DELETE;
    doAction();

   }
  };
  button5.setOnClickListener(listener5);
  Button button6 = (Button) findViewById(R.id.Button06);
  OnClickListener listener6 = new OnClickListener() {
   @Override
   public void onClick(View v) {
    cmd = CMD_DELETE_ALL;
    doAction();

   }
  };
  button6.setOnClickListener(listener6);
  mHandler = new Handler();
    }
 int cnt = 0;
 private Handler mHandler;
 int cmd = 0;
 final int CMD_ADD = 1;
 final int CMD_UPDATE = 2;
 final int  CMD_QUERY= 3;
 final int CMD_QUERY_ALL = 4;
 final int CMD_DELETE = 5;
 final int CMD_DELETE_ALL = 6;
 People people=new People();
 final static String projection[]=new String[]
                                       {"_id","name","phone","age"};
 class DatabaseThread implements Runnable {
  public void run() {
   if (cmd == CMD_ADD) {
    people.name="robin"+System.currentTimeMillis()%100;
    people.phone=""+System.currentTimeMillis();
    people.age=1;
    ContentValues values=new ContentValues();
    values.put("name", people.name);
    values.put("phone", people.phone);
    values.put("age", people.age);
    Uri uri=getContentResolver().insert(MyProvider.content_URI, values);
    people.id=ContentUris.parseId(uri);
    Log.i("hubin",uri.toString());
   } else if (cmd == CMD_UPDATE) {
    ContentValues values=new ContentValues();
    people.phone=""+System.currentTimeMillis();
    values.put("phone", people.phone);
    Uri uri=ContentUris.withAppendedId(MyProvider.content_URI, people.id);
    getContentResolver().update(uri,values,null,null);
   } else if (cmd == CMD_QUERY) {
    Uri uri=ContentUris.withAppendedId(MyProvider.content_URI, people.id);
    Cursor c=
getContentResolver().query(uri, projection, null, null, null);
    People p=get(c);
    printPeople(p);
   } else if (cmd == CMD_QUERY_ALL) {
    Uri uri=MyProvider.content_URI;
    Cursor c=
getContentResolver().query(uri, projection, null, null, null);
    List<People> list=getAll(c);
    int total=list.size();
    for(int i=0;i<total;i++)
    {
     printPeople(list.get(i));
    }
   }
   else if (cmd==CMD_DELETE)
   {
    Uri uri=ContentUris.withAppendedId(MyProvider.content_URI, people.id);
    getContentResolver().delete(uri, null, null);
   }
   else if (cmd==CMD_DELETE_ALL)
   {
    Uri uri=MyProvider.content_URI;
    getContentResolver().delete(uri, null, null);
   }
   cnt++;
  }
 }
 void printPeople(People p)
 {
 Log.i(tag, "id:"+p.id);
 Log.i(tag, "name:"+p.name);
 Log.i(tag,"phone:"+p.phone);
 Log.i(tag,"age:"+p.age);
 }
 DatabaseThread dataDealer=new DatabaseThread();
 void doAction() {
  mHandler.post(dataDealer);
 }
 public People get(Cursor c)
 {
  People people=new People();
  try{
   Log.i(tag,"count:"+c.getCount());
   if(c.getCount()>0)
   {
    c.moveToFirst();
    people=new People();
    people.id=c.getLong(0);
    people.name=c.getString(1);
    people.phone=c.getString(2);
    people.age=c.getInt(3);
   }
  }catch(SQLException e)
  {
   Log.i(tag,"",e);
  }
  finally
  {
   if(c!=null&&!c.isClosed())
   {
    c.close();
   }
  }
  return people;
 }
 public List<People> getAll(Cursor c)
 {
  ArrayList<People> ret=new ArrayList<People>();
  try
  {
   int count=c.getCount();
   c.moveToFirst();
   People people;
   for(int i=0;i<count;i++)
   {
    people=new People();
    people.id=c.getLong(0);
    people.name=c.getString(1);
    people.phone=c.getString(2);
    people.age=c.getInt(3);
    ret.add(people);
    c.moveToNext();
   }
   
  }catch(SQLException e)
  {
   Log.i(tag,"",e);
  }
  finally
  {
   if(c!=null&&!c.isClosed())
   {
    c.close();
   }
  }
  return ret;
 }
}
注意:Cursor c不用時要關掉。
文件AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.teleca.provider"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Hello"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
<provider android:syncable="true" android:name="MyProvider" android:authorities="com.teleca.PeopleProvider"></provider>
</application>
    <uses-sdk android:minSdkVersion="7" />
</manifest>
list_row.xml文件 
<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:id="@+id/list_row" />
main.xml文件
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > 
<ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00FF00" android:layout_weight="1" android:drawSelectorOnTop="false"/> 
<Button android:text="@+string/Add" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="@+string/DeleteAll" android:id="@+id/Button02" android:layout_width="wrap_content"></Button>
</LinearLayout>
需要的權限:
<uses-permission android:name="android.permission.READ_CONTACTS" />
 <uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章