Changes

Jump to: navigation, search

Understanding Android 6 Content Providers

2,712 bytes removed, 18:29, 4 January 2016
no edit summary
<google>BUY_ANDROID_STUDIO6</google>
When implementing the insert, query, update and delete methods in the content provider, it will be the responsibility of these methods to identify whether the incoming URI is targeting a specific row in a table, or references multiple rows and act accordingly. This can potentially be a complex task given that a URI can extend to multiple levels. This process can, however, be eased significantly by making use of the UriMatcher class as will be outlined in the next chapter.
 
== The Content Resolver ==
 
Access to a content provider is achieved via a ContentResolver object. An application can obtain a reference to its content resolver by making a call to the getContentResolver() method of the application context.
 
The content resolver object contains a set of methods that mirror those of the content provider (insert, query, delete etc). The application simply makes calls to the methods, specifying the URI of the content on which the operation is to be performed. The content resolver and content provider objects then communicate to perform the requested task on behalf of the application.
 
== The <provider> Manifest Element ==
 
In order for a content provider to be visible within an Android system, it must be declared within the Android manifest file for the application in which it resides. This is achieved using the <provider> element, which must contain the following items:
 
* '''android:authority''' – The full authority URI of the content provider. For example com.example.mydbapp.mydbapp.myprovider.
* '''android:name''' – The name of the class that implements the content provider. In most cases, this will use the same value as the authority.
 
Similarly, the <provider> element may be used to define the permissions that must be held by client applications in order to qualify for access to the underlying data. If no permissions are declared, the default behavior is for permission to be allowed for all applications.
 
Permissions can be set to cover the entire content provider, or limited to specific tables and records.
 
== Summary ==
 
The data belonging to an application is typically private to the application and inaccessible to other applications. In situations where the data needs to be shared, it is necessary to set up a content provider. This chapter has covered the basic elements that combine to enable data sharing between applications and outlined the concepts of the content provider, content URI and content resolver.
 
In the next chapter, the Android Studio Database example application created previously will be extended to make the underlying product data available via a content provider.
 
 
<google>BUY_ANDROID_STUDIO6</google>
 
 
<hr>
<table border="0" cellspacing="0" width="100%">
<tr>
<td width="20%">[[An Android 6 SQLite Database Tutorial|Previous]]<td align="center">[[Android Studio Development Essentials - Android 6 Edition|Table of Contents]]<td width="20%" align="right">[[Implementing an Android Content Provider in Android Studio|Next]]</td>
<tr>
<td width="20%">An Android 6 SQLite Database Tutorial<td align="center"><td width="20%" align="right">Implementing an Android Content Provider in Android Studio</td>
</table>
== The Content Resolver ==

Navigation menu