Sharing data across Programs

Source: Internet
Author: User

First, the content provider

Content Provider is primarily used to implement data sharing among different applications, providing a complete set of mechanisms that allow one program to access data from another program while guaranteeing the security of the data being accessed.

There are two general uses of content providers, one is to use an existing content provider to read and manipulate data in the corresponding program, and to create your own content provider to provide external access to the data of our programs.

Second, the basic usage of contentresolver

For each application, if you want to access the data that is shared in the content provider, you must use the Contentresolve class to get an instance of the class through the Getcontentresolver () method in the context. Contentresolver provides a series of methods for CRUD operations on data, where the Insert () method is used to add data, the update () method is used to update the data, and the Delete () method is used to delete the data, and the query () method is used to query the data.

Third, read the system contact

The read system contact is also required to declare permissions , so modify the code in Androidmanifest.xml:

<Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.contactstest"Android:versioncode= "1"Android:versionname= "1.0" >...<uses-permissionAndroid:name= "Android.permission.READ_CONTACTS" />
<uses-permission android:name= "Android.permission.CALL_PHONE"/>
...</Manifest>

Read the Contact function

    Private voidreadcontacts () {cursor cursor=NULL; Try{cursor=getcontentresolver (). Query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL,NULL,NULL,NULL);  while(Cursor.movetonext ()) {String DisplayName=cursor.getstring (Cursor.getcolumnindex (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String Number=cursor.getstring (Cursor.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER)); Contactslist.add (DisplayName+ "\ n" +Number ); }        } Catch(Exception e) {//Todo:handle Exception}finally{            if(cursor!=NULL) {cursor.close (); }        }    }

By reading the contacts, add click events, jump directly to the dial interface, to make the call function (note registration rights):

Contactsview.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> arg0, View arg1,intposition,LongArg3) {                //TODO auto-generated Method Stub//Get string is name + line break + phone numberString contactinfo=Contactslist.get (position); String Contact=contactinfo.substring (Contactinfo.indexof ("\ n") +1); Intent Intent=NewIntent (Intent.action_call, Uri.parse ("Tel:" +Contact ));            StartActivity (Intent); }        });

Summarize:

Cursor:

    • The Cursor is a collection of each row.
    • Use Movetofirst () to locate the first row.
    • You must know the name of each column.
    • You must know the data type of each column.
    • The Cursor is a random data source.
    • All data is obtained by subscript.

Sharing data across Programs

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.