当前位置:网站首页>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);}


边栏推荐
- Hdu1573 x problem [univariate linear congruence equations]
- [basic knowledge of deep learning - 46] Bayesian theorem and conditional probability formula
- TSMC 5nm is about to mass produce: Apple A14 monopolizes 70% of the production capacity, and Huawei Kirin 1020 takes 30%
- 文件操作防护
- 反超华为?爱立信已拿下超过75份5G商用合同
- Come to sword finger offer 03. Repeated numbers in the array
- Make your chat bubbles colorful
- I want to consult. Our maxcompute spark program needs to access redis, development environment and production environment redis
- File operation protection
- 【深度学习基础知识 - 38】L1正则化和L2正则化的区别
猜你喜欢
![[basic knowledge of deep learning - 45] distance calculation methods commonly used in machine learning](/img/6c/b0c2ea667ac361c13d38c8f5e6e5f1.png)
[basic knowledge of deep learning - 45] distance calculation methods commonly used in machine learning

Introduction to Flink operator

Chinese character search Pinyin wechat applet project source code

Detailed explanation of the underlying data structure of redis

Debian recaptured the "debian.community" domain name, but it's still not good to stop and rest

Combinatorics -- permutation and combination

Release Samsung 3J1 sensor: the code implies that the safety of pixel 7 face recognition will be greatly increased

Influxdb series (IV) TSM engine (storage principle)

Hyperledger caliper is built on fabric for performance test

Use of jvisualvm
随机推荐
File operation protection
MySQL time zone problem
[basic knowledge of deep learning - 39] comparison of BN, LN and WN
【深度学习基础知识 - 47】贝叶斯网络与朴素贝叶斯
SQLServer 2008中事务日志已满问题处理
Oracle 简单的高级查询
[daily accumulation - 06] view CUDA and cudnn versions
Flink introduction and operation architecture
[basic knowledge of deep learning - 48] characteristics of Bayesian network
Hdu1573 x problem [univariate linear congruence equations]
VS2017#include 'xxx.h'
SharePreference(存储)
A low code development platform that brings high-value user experience
[basic knowledge of deep learning - 50] PCA dimensionality reduction principal component analysis
【日常积累 - 06】查看cuda和cudnn版本
Incluxdb series (III) detailed explanation of incluxdb configuration file
Introduction to Flink operator
几种无线协议简介
【深度学习基础知识 - 49】Kmeans
【深度学习基础知识 - 39】BN、LN、WN的比较