新聞中心
本篇文章給大家分享的是有關(guān)Android中Content Provider的作用是什么,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

讓客戶(hù)滿(mǎn)意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:國(guó)際域名空間、虛擬空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、長(zhǎng)沙縣網(wǎng)站維護(hù)、網(wǎng)站推廣。
Content Provider(內(nèi)容提供器)
在內(nèi)容提供器中,有別于文件儲(chǔ)存這類(lèi)的是,他可以指定想要共享的部分,而不是全局共享
對(duì)于每一個(gè)應(yīng)用程序來(lái)說(shuō),如果想要訪問(wèn)內(nèi)容提供器里共享的數(shù)據(jù),就一定要借助ContentResolver類(lèi),通過(guò)getContent -Resolver獲取實(shí)例,在ContentResolver中提供了一系CRUD(增刪改查)的方法進(jìn)行操作其中
insert,添加數(shù)據(jù)
update,更新數(shù)據(jù)
delete,刪除數(shù)據(jù)
query,查詢(xún)數(shù)據(jù)
不同于SQLiteDatebase,ContentResolver中的CRUD是使用一個(gè)Uri參數(shù)代替,這個(gè)參數(shù)被稱(chēng)為內(nèi)容參數(shù),由兩部分組成:authority和path。authority主要用來(lái)對(duì)不同應(yīng)用進(jìn)行區(qū)分,path用于對(duì)同一個(gè)應(yīng)用的不同表進(jìn)行區(qū)分
例如:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addData = (Button) findViewById(R.id.add_data);
addData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 添加數(shù)據(jù)
Uri uri = Uri.parse("content://com.example.databasetest.provider/book");
ContentValues values = new ContentValues();
values.put("name", "A Clash of Kings");
values.put("author", "George Martin");
values.put("pages", 1040);
values.put("price", 55.55);
Uri newUri = getContentResolver().insert(uri, values);
newId = newUri.getPathSegments().get(1);
}
});
Button queryData = (Button) findViewById(R.id.query_data);
queryData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 查詢(xún)數(shù)據(jù)
Uri uri = Uri.parse("content://com.example.databasetest.provider/book");
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String name = cursor.getString(cursor. getColumnIndex("name"));
String author = cursor.getString(cursor. getColumnIndex("author"));
int pages = cursor.getInt(cursor.getColumnIndex ("pages"));
double price = cursor.getDouble(cursor. getColumnIndex("price"));
Log.d("MainActivity", "book name is " + name);
Log.d("MainActivity", "book author is " + author);
Log.d("MainActivity", "book pages is " + pages);
Log.d("MainActivity", "book price is " + price);
}
cursor.close();
}
}
});
Button updateData = (Button) findViewById(R.id.update_data);
updateData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 更新數(shù)據(jù)
Uri uri = Uri.parse("content://com.example.databasetest.provider/book/" + newId);
ContentValues values = new ContentValues();
values.put("name", "A Storm of Swords");
values.put("pages", 1216);
values.put("price", 24.05);
getContentResolver().update(uri, values, null, null);
}
});
Button deleteData = (Button) findViewById(R.id.delete_data);
deleteData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 刪除數(shù)據(jù)
Uri uri = Uri.parse("content://com.example.databasetest.provider/book/" + newId);
getContentResolver().delete(uri, null, null);
}
});
}似乎很簡(jiǎn)單的樣子,但是這樣就一定安全嗎?或者說(shuō)是所有人都用這個(gè)方法去共享數(shù)據(jù)的嗎,如果不是呢?這就要說(shuō)到自己創(chuàng)建內(nèi)容提供器
繼承ContentProvider,然后實(shí)現(xiàn)他的六個(gè)抽象方法即可
onCreate()
query(Uri uri,String[] projection,String selection,String[] selectionArgs,String sortOrder)
insert(Uri uri,ContentValues values)
updaya(Uri uri,ContentValues values)
updata(Uri uri,ContenValues values,String selection,String[]selectionArgs)
delete(Uri uri,String selection,String[] selectionArgs)
getType(Uri uri)
以上就是Android中Content Provider的作用是什么,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前文章:Android中ContentProvider的作用是什么
文章來(lái)源:http://www.dlmjj.cn/article/iepgii.html


咨詢(xún)
建站咨詢(xún)
