A brief talk on Android development the use of content provider provider

Source: Internet
Author: User

Good New Year for everyone. Bloggers have just returned to Guangzhou will be all kinds of busy. Bo Master changed a job, so yesterday came to Guangzhou busy looking for a house, but last night has been done. Time is more urgent, this article will directly how to use the provider.

Content providers are typically used for communication between processes, and when an app needs to access the database content of another app, it needs to use the content provider. In order to save time, Bo Master directly lazy on the code, directly to take the blogger before written a text message project to ON.

First, define a content provider

Package Com.freedom.intelligencesms.provider;import Com.freedom.intelligencesms.db.groupopenhelper;import Com.freedom.intelligencesms.utils.smsutils;import Android.content.contentprovider;import Android.content.contenturis;import Android.content.contentvalues;import Android.content.urimatcher;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Android.net.Uri;import Android.text.textutils;public class Groupcontentprovider extends ContentProvider {//Authorized address private static final String authority = "Com.freedom.intelligencesms.provider.GroupContentProvider";p rivate static final int groups_insert = 0; private static final int groups_query_all = 1;private static final int groups_update = 2;private static final int thread_g Roup_insert = 3;private static final int thread_group_query_all = 4;private static final int thread_group_query = 5;privat e static final int groups_delete = 6;private static urimatcher urimatcher;private groupopenhelper mopenhelper;private Fina LString groups_table = "GROUPS";p rivate final String thread_group_table = "Thread_group"; static {//define a match Urimatcher = new Urimatcher (Urimatcher.no_match);//content://com.freedom.intelligencesms.provider.groupcontentprovider/groups/ Inserturimatcher.adduri (Authority, "Groups/insert", groups_insert);//content:// Com.freedom.intelligencesms.provider.groupcontentprovider/groups/urimatcher.adduri (AUTHORITY, "groups", GROUPS_ Query_all);//content://com.freedom.intelligencesms.provider.groupcontentprovider/groups/ Updateurimatcher.adduri (Authority, "Groups/update", groups_update);//content:// com.freedom.intelligencesms.provider.groupcontentprovider/groups/delete/#uriMatcher. Adduri (AUTHORITY, "groups/ delete/# ", groups_delete);//content://com.freedom.intelligencesms.provider.groupcontentprovider/thread_group/ Inserturimatcher.adduri (Authority, "Thread_group/insert", thread_group_insert);//content:// Com.freedom.intelligencesms.provider.groupcontentprovider/thread_groupurimatcher.adduri (AUTHORITY, "Thread_group", thread_group_query_all);//Content://com.freedom.intelligencesms.provider.groupcontentprovider /thread_group/#uriMatcher. Adduri (Authority, "thread_group/#", Thread_group_query);} @Overridepublic Boolean onCreate () {//Get database Mopenhelper = Groupopenhelper.getinstance (GetContext ()); return false;} @Overridepublic Cursor query (Uri uri, string[] projection, string selection,string[] Selectionargs, string sortOrder) {SQ Litedatabase db = Mopenhelper.getreadabledatabase (); switch (Urimatcher.match (URI)) {Case Groups_query_all:if ( Db.isopen ()) {cursor cursor = db.query (groups_table, projection, selection, Selectionargs, NULL, NULL, sortOrder); cursor . Setnotificationuri (GetContext (). Getcontentresolver (), Smsutils.groups_query_all_uri); return cursor;} Break;case thread_group_query_all:if (Db.isopen ()) {return db.query (thread_group_table, projection, selection, Selectionargs, NULL, NULL, SortOrder);} Break;case thread_group_query:if (Db.isopen ()) {Long id = Contenturis.parseid (URI); String WHere = "_id =" + id;if (! Textutils.isempty (selection)) {where = Selection + "and" + where;} Return Db.query (thread_group_table, projection, where, Selectionargs, NULL, NULL, SortOrder);} Break;default:throw new IllegalArgumentException ("Unknow uri:" + uri);} return null;} @Overridepublic String getType (Uri uri) {return null;} @Overridepublic uri insert (URI uri, contentvalues values) {sqlitedatabase db = mopenhelper.getwritabledatabase (); switch (Urimatcher.match (URI)) {Case Groups_insert:if (Db.isopen ()) {Long id = db.insert (groups_table, null, values); GetContext (). Getcontentresolver ( ). Notifychange (Smsutils.groups_query_all_uri, null); return Contenturis.withappendedid (URI, id);} Break;case thread_group_insert:if (Db.isopen ()) {Long id = db.insert (thread_group_table, null, values); return Contenturis.withappendedid (URI, id);} Break;default:throw new IllegalArgumentException ("Unknow uri:" + uri);} return null;} @Overridepublic int Delete (URI Uri, String selection, string[] selectionargs) {switch (Urimatcher.match (URI)) {case Groups_delete:sqlitedatabase db = Mopenhelper.getwritabledatabase (); if (Db.isopen ()) {Long group_id = Contenturis.parseid (URI); String where = "_id =" + group_id;if (! Textutils.isempty (selection)) {where = Selection + "and" + where;} int count = Db.delete (groups_table, where, Selectionargs), GetContext (). Getcontentresolver (). Notifychange ( Smsutils.groups_query_all_uri, null); where = "group_id =" + Group_id;db.delete (thread_group_table, where, null); return Count;} Break;default:throw new IllegalArgumentException ("Unknow uri:" + uri);} return 0;} @Overridepublic int update (URI uri, contentvalues values, String selection,string[] selectionargs) {switch ( Urimatcher.match (URI)) {case Groups_update:sqlitedatabase db = Mopenhelper.getwritabledatabase (); if (Db.isOpen ()) { int count = Db.update (groups_table, values, selection, Selectionargs); GetContext (). Getcontentresolver (). Notifychange (Smsutils.groups_query_all_uri, null); return count;} Break;default:throw New IllegalArgumentException ("Unknow uri:" + uri);} return 0;}}
Register in the manifest file:

   <provider            android:name= ". Provider. Groupcontentprovider "            android:authorities=" Com.freedom.intelligencesms.provider.GroupContentProvider ">        </provider>

In this way, a content provider is ready to write. When we need to call.

String Path = Content://sms/uri Uri = uri.parse (path) contentresolver resolver = Getcontentresolver (); cursor cursor = resolver.query (uri ...). )

Sometimes, we need to listen to the data changes in the database, then we need to use the content observer. Also directly on the use of code:

Content Observer Contentresolver resolver = Getcontentresolver (); Resolver.registercontentobserver (Uri,true,new MyOberserver ( New Handler) Private class Myobserver extends Contentobserver{public myobserver (Handler Handler) {super (Handler)}// Notify public void OnChange (Boolean selfchange) {Super.onchange (Selfchange) when the content viewer discovers that the database is changing, and/or observes that there is a data change in the message mailbox; Uri uri = uri.parse ("content://sms"); Contentresolver resolver = Getcontentresolver (); cursor cursor = resolver.query (uri,new string[]{"Address", "date", "" type "," Body "},null,null,null);// Gets the specified content from the SMS database while (Cursor.movertonext ()) {String address = cursor.getstring (0) ...} Get all SMS Messages Cursor.movetolast ()//point to the last SMS Cursor.movetofirst ()//point to the latest message}}


Due to insufficient preparation, time rushed busy, this article is not very good, hope you understand.


A brief talk on Android development the use of content provider provider

Related Article

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.