作为android四大组件之一,ContentProvider可能是四大组件中我们用到最少的。
它作为跨进程数据共享来使用,而我们开发app的时候,基本上是独立的,不会与其他的app发生数据间的通讯。
但如果两个或者多个app需要共享一个数据源的时候,ContentProvider就显的非常必要且高安全性,因为我们可以控制数据源的哪些数据可以被访问,哪些不能被访问。
package com.example.contentporvider_csdn;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.view.Menu;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView listView;
List list;
ArrayAdapter arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
listView=(ListView)findViewById(R.id.mylistview);
list=new ArrayList();
arrayAdapter=new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(arrayAdapter);
getContentProvider();
}
private void getContentProvider()
{
Cursor cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null,null);
if(cursor!=null)
{
while (cursor.moveToNext()) {
String displayNameString=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String numberString=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
list.add(displayNameString+"\n"+numberString);
}
cursor.close();
}
}
}
对应的布局如下:
对比我们以前的代码,我们发现唯一有些特殊的地方就在于:
Cursor cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null,null);
对这个获取数据方式,系统的解释是:
以上,就是使用provider要注意的地方了。简单的来说,就两句代码:
Cursor cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null,null);
权限声明:
package com.example.contentporvider_csdn;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
public class MyContentPorvider extends ContentProvider{
public static final int DIR=1;
public static final int ITEM=2;
static String tableString="one";
private static UriMatcher uriMatcher;
static
{
uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI("com.example.contentporvider_csdn.provider", tableString, DIR);
uriMatcher.addURI("com.example.contentporvider_csdn.provider", tableString+"/#", ITEM);
}
SqliteImp sqliteImp;
public MyContentPorvider() {
// TODO Auto-generated constructor stub
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
sqliteImp=new SqliteImp(getContext(), tableString, null, 1);
return true;
}
@Override
public Cursor query(Uri paramUri, String[] paramArrayOfString1,
String paramString1, String[] paramArrayOfString2,
String paramString2) {
// TODO Auto-generated method stub
Cursor cursor=null;
switch (uriMatcher.match(paramUri)) {
case DIR:
cursor=sqliteImp.getWritableDatabase().query(tableString, paramArrayOfString1, paramString1, paramArrayOfString2, null, null, paramString2);
break;
case ITEM:
long id= ContentUris.parseId(paramUri);
String whereString="id="+id+" and "+paramString1;
cursor=sqliteImp.getWritableDatabase().query(tableString, paramArrayOfString1, whereString, paramArrayOfString2, null, null, paramString2);
break;
default:
break;
}
return cursor;
}
@Override
public String getType(Uri paramUri) {
// TODO Auto-generated method stub
String typeString="";
switch (uriMatcher.match(paramUri)) {
case DIR:
typeString="vnd.android.cursor.dir/vnd.com.example.contentporvider_csdn.provider";
break;
case ITEM:
typeString="vnd.android.cursor.item/vnd.com.example.contentporvider_csdn.provider";
break;
default:
break;
}
return typeString;
}
@Override
public Uri insert(Uri paramUri, ContentValues paramContentValues) {
// TODO Auto-generated method stub
Uri uri=null;
long rowid= sqliteImp.getWritableDatabase().insert(tableString, null, paramContentValues);
switch (uriMatcher.match(paramUri)) {
case DIR:
uri=ContentUris.withAppendedId(paramUri, rowid);
break;
case ITEM:
uri=paramUri;
break;
default:
break;
}
return uri;
}
@Override
public int delete(Uri paramUri, String paramString,
String[] paramArrayOfString) {
// TODO Auto-generated method stub
int result=0;
switch (uriMatcher.match(paramUri)) {
case DIR:
result= sqliteImp.getWritableDatabase().delete(tableString, paramString, paramArrayOfString);
break;
case ITEM:
long id=ContentUris.parseId(paramUri);
paramString="id="+id+" and "+paramString;
result= sqliteImp.getWritableDatabase().delete(tableString, paramString, paramArrayOfString);
break;
default:
break;
}
return result;
}
@Override
public int update(Uri paramUri, ContentValues paramContentValues,
String paramString, String[] paramArrayOfString) {
// TODO Auto-generated method stub
int rowid= 0;
switch (uriMatcher.match(paramUri)) {
case DIR:
rowid= sqliteImp.getWritableDatabase().update(tableString, paramContentValues, paramString, paramArrayOfString);
break;
case ITEM:
long id=ContentUris.parseId(paramUri);
paramString="id="+id+" and "+paramString;
rowid= sqliteImp.getWritableDatabase().update(tableString, paramContentValues, paramString, paramArrayOfString);
break;
default:
break;
}
return rowid;
}
}
数据源的提供,我们通过sqlite的数据库来实现。建立一个示意的数据表如下:
package com.example.contentporvider_csdn;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class SqliteImp extends SQLiteOpenHelper{
private String createString="create table one (id integer primary key autoincrement," +
"name text," +
"number text)" +
"";
public SqliteImp(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
arg0.execSQL(createString);
ContentValues contentValues=new ContentValues();
contentValues.put("name", "zhigao");
contentValues.put("number", "15880099999");
arg0.insert("one", null, contentValues);
contentValues.clear();
contentValues.put("name", "kongtiao");
contentValues.put("number", "15880077777");
arg0.insert("one", null, contentValues);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
在这里,我们在新建表one的时候,顺便新增了两条数据,用于后续演示。
uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI("com.example.contentporvider_csdn.provider", tableString, DIR);
uriMatcher.addURI("com.example.contentporvider_csdn.provider", tableString+"/#", ITEM);
(3)uriMatcher.match(paramUri)
sqliteImp=new SqliteImp(getContext(), tableString, null, 1);
获取我们设置的一些数据。
当其他程序想要使用你自定义的contentporvider时,需要声明权限,如下:
package com.example.contentprovidertest_csdn;
import java.util.ArrayList;
import java.util.List;
import android.net.Uri;
import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
ListView listView;
List list;
ArrayAdapter adapter;
Button add,delete,modify,query;
Uri uri=Uri.parse("content://com.example.contentporvider_csdn.provider/one");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
public void initView()
{
listView=(ListView)findViewById(R.id.mylistview);
list=new ArrayList();
adapter=new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
add=(Button)findViewById(R.id.add);
delete=(Button)findViewById(R.id.delete);
modify=(Button)findViewById(R.id.modify);
query=(Button)findViewById(R.id.query);
add.setOnClickListener(this);
delete.setOnClickListener(this);
modify.setOnClickListener(this);
query.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.add:
ContentValues contentValues=new ContentValues();
contentValues.put("name", "yang_add");
contentValues.put("number", "15880076777");
Uri newuri= getContentResolver().insert(uri, contentValues);
Toast.makeText(MainActivity.this, newuri.toString(), 0).show();
break;
case R.id.delete:
int result= getContentResolver().delete(uri, "name=?", new String[]{"yang_add"});
Toast.makeText(MainActivity.this, result+"", 0).show();
break;
case R.id.modify:
ContentValues cValues=new ContentValues();
cValues.put("name", "yang_modify");
cValues.put("number", "15880076000");
int result_modify=getContentResolver().update(uri, cValues, "name=?", new String[]{"yang_add"});
Toast.makeText(MainActivity.this, result_modify+"", 0).show();
break;
case R.id.query:
Cursor cursor= getContentResolver().query(uri, null, null, null, null);
if(cursor!=null)
{
list.clear();
while(cursor.moveToNext())
{
String nameString=cursor.getString(cursor.getColumnIndex("name"));
String numString=cursor.getString(cursor.getColumnIndex("number"));
list.add(nameString+"\n"+numString);
}
cursor.close();
adapter.notifyDataSetChanged();
}
break;
default:
break;
}
}
}
对于使用ContentProvider,是很简单的一个过程,主要还是在URI这个参数~~。
源码演示地址:
http://download.csdn.net/detail/yangzhaomuma/9314739