android四大組件之contentProvider
ContentProvider主要用於各應用程序之間數據的共享
訪問共享的數據需要Context的getContentResolver方法獲取ContentResolver對象,該對象提供了增刪改查的
數據的方法,這些方法的參數有個uri,uri可以用Uri.parse(String)得到,這個字符串參數的格式通常爲
content://包名.provider/表名
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://com.hao.contentProvider.provider/tel");
查詢://參數與sql語句基本對應
//uri:指定查詢某個程序下的某個表
//projection:查詢的列名
//selection:where條件
//selectionArgs:爲where中佔位符指定具體值
//orderBy:查詢結果排列方式
Cursor cursor = resolver.query(uri, projection, selection, selectionArgs, sortOrder);
返回的cursor對象,我們要用遊標遍歷取出數據
if(cursor != null){
while(cursor.moveToNext()){
String str = cursor.getString(cursor.getColumnIndex("xxx"));
}
cursor.close();
}
//增加
ContentValues values = new ContentValues();
values.put("phone", "666");
resolver.insert(uri, values);
//改
values.clear();
values.put("phone", "123");
resolver.update(uri, values, "phone = ?", new String[]{"666"});
//刪
resolver.delete(uri, "phone = ?", new String[]{"123"});
介紹了獲取數據,那如何將自己的數據共享出去呢?我們需要定義自己的Provider類繼承ContentProvider
public class MyProvider extends ContentProvider{
@Override
public boolean onCreate() {
//
return false;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
return null;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
}
身爲四大組件之一,那當然還要在AndroidManifast.xml中配置,其中authorities指定權限,exported配置
是否可以被其他程序訪問
<provider
android:name="com.hao.firstcode_contentprovider.MyProvider"
android:exported="true">
</provider>
訪問共享的數據需要Context的getContentResolver方法獲取ContentResolver對象,該對象提供了增刪改查的
數據的方法,這些方法的參數有個uri,uri可以用Uri.parse(String)得到,這個字符串參數的格式通常爲
content://包名.provider/表名
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://com.hao.contentProvider.provider/tel");
查詢://參數與sql語句基本對應
//uri:指定查詢某個程序下的某個表
//projection:查詢的列名
//selection:where條件
//selectionArgs:爲where中佔位符指定具體值
//orderBy:查詢結果排列方式
Cursor cursor = resolver.query(uri, projection, selection, selectionArgs, sortOrder);
返回的cursor對象,我們要用遊標遍歷取出數據
if(cursor != null){
while(cursor.moveToNext()){
String str = cursor.getString(cursor.getColumnIndex("xxx"));
}
cursor.close();
}
//增加
ContentValues values = new ContentValues();
values.put("phone", "666");
resolver.insert(uri, values);
//改
values.clear();
values.put("phone", "123");
resolver.update(uri, values, "phone = ?", new String[]{"666"});
//刪
resolver.delete(uri, "phone = ?", new String[]{"123"});
介紹了獲取數據,那如何將自己的數據共享出去呢?我們需要定義自己的Provider類繼承ContentProvider
public class MyProvider extends ContentProvider{
@Override
public boolean onCreate() {
//
return false;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
return null;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
}
身爲四大組件之一,那當然還要在AndroidManifast.xml中配置,其中authorities指定權限,exported配置
是否可以被其他程序訪問
<provider
android:name="com.hao.firstcode_contentprovider.MyProvider"
android:exported="true">
</provider>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.