Data sharing between Android applications

Source: Internet
Author: User

How does Android achieve data sharing between applications? An application can completely expose its own data, which is invisible to the outside world. It does not need to see how the data exposed by the application is stored, or whether it uses databases or files, or through the Internet, all these are not important. What is important is that the outside world can use this set of standards and unified interfaces to deal with the data in this program, for example: add (insert) delete, query, and update require certain permissions. How to expose application data? Android provides ContentProvider. A program can completely expose its data by implementing an abstract interface of the Content provider, and Content providers exposes data in a way similar to a table in a database. Content providers stores and retrieves data, which can be accessed by all applications. This is the only way to share data between applications. To make the data of an application public, you can create a Content provider of your own or add your data to an existing Content provider, the premise is that you have the same data type and the permission to write Content provider. How can I obtain data exposed by other applications through a set of standard and unified interfaces? Android provides ContentResolver, and external programs can access the data provided by ContentProvider through the ContentResolver interface. This article describes how to obtain data shared by other applications, such as information in the phone book of Android phones. What is URI? Before learning how to obtain ContentResolver, you must know the following terms: URI. URI is the definition of network resources. It is given A broader meaning in Android. Let's take A look at the following example: divide it into four parts: A, B, C, and D:: the standard prefix, which indicates that a Content Provider controls the data and cannot be changed. B: The URI identifier, which defines which Content Provider provides the data. For third-party applications, to ensure the uniqueness of the URI identifier, it must be a complete, lowercase class name. This identifier is described in the authorities attribute of the <provider> element: <provider name = ". transportationProvider "authorities =" com. example. transportationprovider "...> c: path. Content Provider uses these paths to determine the type of data to be generated. The URI may not include the path or multiple data types. D: If the URI contains, indicates the ID of the record to be obtained. If there is no ID, all records are returned. Because the URI is usually long and sometimes error-prone, it is hard to understand. Therefore, some helper classes are defined in Android and some constants are defined to replace these long strings, such as: People. after reading the description of CONTENT_URIContentResolver, you will surely understand that ContentResolver uses URI to query the data provided in ContentProvider. In addition to URI, you must also know the name of the data segment to be obtained and the Data Type of the Data Segment. If you need to obtain a specific record, you must know the ID of the current record, that is, the D part of the URI. As mentioned above, Content providers exposes data in a way similar to tables in a database, so ContentResolver will also use database-like operations to obtain data from Content providers. The following describes the main interfaces of ContentResolver: return value function declaration final Uri insert (Uri url, ContentValues values) Inserts a row into a table at the given URL. final int delete (Uri url, String where, String [] selectionArgs) Deletes row (s) specified by a content URI. final Cursor query (Uri uri, String [] projection, String selection, String [] selectionArgs, String sortOrder) Query the given URI, returning a Cursor over the result Set. final int update (Uri uri, ContentValues values, String where, String [] selectionArgs) Update row (s) in a content URI. do you think the operation is basically the same as that of the database? This is the case. For detailed parsing, refer to the description in Android SQLite parsing, not here. Last question: how to obtain ContentResolver? Call getContentResolver (), for example: ContentResolver cr = getContentResolver (); Create a ContentResolver instance. The following describes how to obtain, use ContentResolver, start es, and create a complete instance: open showcontent. java: package moandroid. showcontact; import android. app. listActivity; import android. database. cursor; import android. OS. bundle; import android. provider. contacts. phones; import android. widget. listAdapter; import android. widget. simpleCursorA Dapter; public class showcontact extends ListActivity {protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); Cursor c = getContentResolver (). query (Phones. CONTENT_URI, null, null); startManagingCursor (c); www.2cto.com ListAdapter adapter = new SimpleCursorAdapter (this, android. r. layout. simple_list_item_2, c, new String [] {Phones. NAME, Phones. NUMBER }, New int [] {android. r. id. text1, android. r. id. text2}); setListAdapter (adapter) ;}} then in AndroidManifest. add the following license before the <application> element in XML: <uses-permission android: name = "android. permission. READ_CONTACTS "/> finally run the program. After the simulator is started, click Menu to return to the Home page. On the Contacts tab, select Contacts and add two Contacts. Return to Home and select moandroid. when showcontact is run, the newly added two contacts are displayed on the interface as follows: summary shows that ContentResolver greatly facilitates data sharing between applications, how to expose the data of an application to another application is described in ContentProvider, which is used by Android applications in the next article.

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.