当前位置:网站首页>ContentProvider of four components
ContentProvider of four components
2022-07-27 19:52:00 【Ashurol】
First of all, let's understand what is ContentProvider, Simply speaking , occasionally android When the application needs to share internal data with other data , At this time ContentProvider With some kind of Uri Provide data to the outside world in the form of , Allow other applications to access or modify data , At this time, other applications will use ContentResolver according to Uri To access the data specified by the operation . in other words ContentProvider The function of is to expose the data available for operation .
// Let's define a ContentProvider Subclasses of , This class does not access the underlying data , Only for understanding
public class MyContentProvider extends ContentProvider{
// stay ContetnProvider Called after creation
public boolean onCreate() {
// TODO Auto-generated method stub
return false;
}
// Returns the current uri Of MIME type , If it's time to URI The corresponding data may include multiple records
// that MIME Type string That is to say vnd.android.dir/ start
// If it's time to URI The corresponding data has only one record The MIME Type string That is to say vnd.android.cursor.item/ start
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
// The following methods can be built according to the development needs , For example, you can add and delete
// according to Uri Insert Values Corresponding data
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
return null;
}
// according to Uri Delete selection Appoint All records matched by the condition of
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
// according to uri modify selection All records matching the specified conditions
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
// according to uri Query out selection All records matching the specified conditions , And you can specify which columns to query In what way (order) Sort
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
return null;
}
}// stay ContentProvider Before being called by other programs , Need to be in manifest This... Is configured in the file ContentProvider // stay application Internal configuration
<provider
android:name=".MyContentProvider"
android:authorities="org.crazyit.provider.firstprovider"
android:exported="true" />// Now let's continue to create a class for another application , To simulate how to access ContentProvider Of Uri Corresponding data
public class FirstResolver extends Activity{
// Define a ContentResolver object
ContentResolver cr;
// Converts a string to Uri
Uri uri=Uri.parse("content://org.crazyit.provider.firstprovider/");
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get one ContentResolver object
cr=getContentResolver();
}
// Monitoring methods
public void query(View source)
{
// call ContentResolver Of query() Method
// What is actually returned is this Uri Corresponding ContentProvider Of query The return value of
Cursor c=cr.query(uri, null, "query_where", null, null);
Toast.makeText(this, " long-range ContentProvider Back to Cursor by :"+c, Toast.LENGTH_SHORT).show();
}
public void insert(View Source)
{
ContentValues va=new ContentValues();
va.put("name", "hahaha");
// call ContentResolver Of insert() Method
// What is actually returned is this Uri Corresponding ContentProvider Of insert() The return value of
Uri newUri=cr.insert(uri, va);
Toast.makeText(this, " long-range ContentProvider Newly inserted record Uri by :"+newUri, Toast.LENGTH_SHORT).show();
}
public void update(View source)
{
ContentValues va=new ContentValues();
va.put("name", "hahaha");
// call ContentResolver Of update() Method
// What is actually returned is this Uri Corresponding ContentProvider Of update() The return value of
int count=cr.update(uri, va, "update_where", null);
Toast.makeText(this, " long-range ContentProvider The number of updated records is :"+count, Toast.LENGTH_SHORT).show();
}
public void delete(View source)
{
// call ContentResolver Of delete() Method
// What is actually returned is this Uri Corresponding ContentProvider Of delete() The return value of
int count=cr.delete(uri, "delete_where", null);
Toast.makeText(this, " long-range ContentProvider The number of records deleted is :"+count, Toast.LENGTH_SHORT).show();
}
}Android Itself provides a lot of ContentProvider, For example, contact information , System multimedia , Developers can also use ContentResolver To call the system ContentProvider Provided query(),insert(),update(), and delete() Other methods .
// Create a project , Yes Android Operate the address book provided by the system
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContentResolver cr = getContentResolver();
Cursor c = cr.query(Contacts.CONTENT_URI, new String[] { Contacts._ID,
Contacts.DISPLAY_NAME }, null, null, null);
if (c != null) {
while (c.moveToNext()) {
int id = c.getInt(c.getColumnIndex("_id"));
Log.i("info", "_id:" + id);
Log.i("info",
"name:" + c.getString(c.getColumnIndex("display_name")));
Cursor c1 = cr.query(Phone.CONTENT_URI, new String[] {
Phone.NUMBER, Phone.TYPE },
Phone.CONTACT_ID + "=" + id, null, null);
// According to contact ID Find out the phone number of the contact
if (c1 != null) {
while (c1.moveToNext()) {
int type = c1.getInt(c1.getColumnIndex(Phone.TYPE));
if (type == Phone.TYPE_HOME) {
Log.i("info",
" Home phone :"
+ c1.getString(c1
.getColumnIndex(Phone.NUMBER)));
} else if (type == Phone.TYPE_MOBILE) {
Log.i("info",
" mobile phone :"
+ c1.getString(c1
.getColumnIndex(Phone.NUMBER)));
}
}
c1.close();
}
// According to the contact's ID Go and find out the email address of the contact
Cursor c2 = cr.query(Email.CONTENT_URI, new String[] {
Email.DATA, Email.TYPE }, Email.CONTACT_ID + "=" + id,
null, null);
if (c2 != null) {
while (c2.moveToNext()) {
int type = c2.getInt(c2.getColumnIndex(Email.DATA));
if (type == Email.TYPE_WORK) {
Log.i("info",
" Work 1 mailbox :"
+ c2.getString(c2
.getColumnIndex(Email.DATA)));
}
}
c2.close();
}
}
c.close();
}
}
}
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContentResolver cr = getContentResolver();
// To contact Insert a row of data
ContentValues values = new ContentValues();
Uri uri = cr.insert(RawContacts.CONTENT_URI, values);
Long raw_contact_id = ContentUris.parseId(uri);
values.clear();
// Insert person name
values.put(StructuredName.RAW_CONTACT_ID, raw_contact_id);
values.put(StructuredName.DISPLAY_NAME, " Zhang Sansan ");
values.put(StructuredName.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
uri = cr.insert(Data.CONTENT_URI, values);
// Insert phone information
values.clear();
values.put(Phone.RAW_CONTACT_ID,raw_contact_id);
values.put(Phone.NUMBER,"13333333333");
values.put(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
uri = cr.insert(Data.CONTENT_URI, values);}


边栏推荐
- RadioGroup(单选框)
- 【日常积累 - 07】cuda多版本切换
- VS2017#include 'xxx.h'
- Intent(有无返回值得跳转)
- Optimization of fixed number of cycles in embedded C language
- 发布自己的npm组件库
- 四大组件之ContentProvider
- [basic knowledge of deep learning - 38] the difference between L1 regularization and L2 regularization
- Oracle XE版安装与用户操作
- Ericsson admitted bribery in China and other five countries and paid a fine of $1.06 billion to the United States
猜你喜欢
随机推荐
【日常积累 - 07】cuda多版本切换
Detailed explanation of the underlying data structure of redis
文件操作防护
[RCTF2015]EasySQL-1|SQL注入
应用程序池已被禁用
反超华为?爱立信已拿下超过75份5G商用合同
一种比读写锁更快的锁,还不赶紧认识一下
【华为云Stack】【大架光临】第13期:管理区解耦架构见过吗?帮政企客户搞定大难题
Samsung will promote a number of risc-v architecture chips, and 5g millimeter wave RF chips will be the first to be adopted
[Huawei cloud stack] [shelf presence] issue 13: have you seen the decoupling architecture of the management area? Help government and enterprise customers solve big problems
英特尔发布Horse Ridge芯片:22nm工艺,能够控制多个量子位
Fabric上搭建Hyperledger caliper进行性能测试
Intel releases horse ridge chip: 22nm process, which can control multiple qubits
Make your chat bubbles colorful
王牌代码静态测试工具Helix QAC 2022.2中的新增功能(2)
[basic knowledge of in-depth learning - 40] Why does CNN have more advantages than DNN in the field of images
SQLServer 2008中事务日志已满问题处理
Publish your own NPM component library
Introduction to Flink operator
Optimization of fixed number of cycles in embedded C language









