Content Provider Learning Notes

Source: Internet
Author: User

# #内容提供者笔记 # #
# # #步骤 # #
1. Create a method that implements the subclass Mycontentprovider of the ContentProvider and overrides the parent class

2, as one of the four components of Android, to register the provider tag in the Manifest.xml file

<provider
Android:name= "Cn.itcast.db.MyContentProvider"
android:authorities= "Cn.itcast.db.persondb" >
</provider>
> which
>**name** is the file path where the MyProvider resides;
>**authorities** is custom content, but preferably as the name implies (should be the package name/database name where the database resides)

3, create Urimatcher object, parameter comfort to **-1**

public static Urimatcher Urimacher = new Urimatche (-1);

4. Increase the URI path in the static code block

static{
Urimacher.adduri ("cn.itcast.db.persondb", "Query", 1);
Urimacher.adduri ("Cn.itcast.db.persondb", "Insert", 2);
Urimacher.adduri ("Cn.itcast.db.persondb", "delete", 3);
Urimacher.adduri ("Cn.itcast.db.persondb", "Update", 4);
Content://cn.itcast.db.persondb/insert
Content://cn.itcast.db.persondb/delete
Content://cn.itcast.db.persondb/update
Content://cn.itcast.db.persondb/query
}
5. When overriding the Query method

Public Cursor query (URI uri, string[] projection, String selection,
String[] Selectionargs, String sortOrder) {
TODO auto-generated Method Stub
int result = Urimacher.match (URI);
if (result==1) {
Sqlitedatabase db = Sqlite.getreadabledatabase ();
return Db.query ("info", projection, selection, Selectionargs, NULL, NULL, sortOrder);
}else{
throw new RuntimeException ("Path error, request data failed");
}
}
6. Get content from content providers in other applications

Uri uri = uri.parse ("Content://cn.itcast.db.persondb/query");
cursor cursor = resolver.query (URI, NULL, NULL, NULL, NULL);
while (Cursor.movetonext ()) {
int id = cursor.getint (cursor.getcolumnindex ("_id"));
String name = cursor.getstring (Cursor.getcolumnindex ("name"));
String phone = cursor.getstring (Cursor.getcolumnindex ("Phone"));
System.out.println (id+ "--" +name+ "--" +phone ");
}
Cursor.close ();
* Used with URI paths added by the content provider *
**//content://cn.itcast.db.persondb/insert
Content://cn.itcast.db.persondb/delete
Content://cn.itcast.db.persondb/update
content://cn.itcast.db.persondb/query**

Content Provider Learning Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.