Android programming course Session objectives Introduction Built-in Custom By Võ Văn Hải Faculty of Information Technologies 2 Content providers Introduction Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process. Such are handled by the methods of the ContentResolver class. A content provider can use different ways to store its data and the data can be stored in a database, in files, or even over a network. 3 4 1
Need for Overall structure Each Android applications runs in its own process with its own permissions which keeps an application data hidden from another application. But sometimes it is required to share data across applications. This is where content providers become very useful. Content providers let you centralize content in one place and have many different applications access it as needed. A content provider behaves very much like a database where you can query it, edit its content, as well as add or delete content using insert(), update(), delete(), and query() methods. In most cases this data is stored in an SQlite database. 5 Application #1 Activity #2 Activity #3 Database Files XML Remote connection Application #2 Application #3 Activity #2 An application accesses the data from a content provider with a ContentResolver client object. This object has methods that call identically-named methods in the provider object, an instance of one of the concrete subclasses of ContentProvider. The ContentResolver methods provide the basic "CRUD" (create, retrieve, update, and delete) functions of persistent storage. The ContentResolver object in the client application's process and the ContentProvider object in the application that owns the provider automatically handle inter-process communication. ContentProvider also acts as an abstraction layer between its repository of data and the external appearance of data as tables. 7 URI: Query Insert ContentResolver URI Update Delete content://com.example.provider/articles/<id> Scheme Authority Path 2
For example, to get a list of the words and their locales from the User Dictionary Provider, you call ContentResolver.query(). The query() method calls the ContentProvider.query() method defined by the User Dictionary Provider. The following lines of code show a ContentResolver.query() call: Built-in s 9 10 Built-in s Get all contacts example Some of Android's most useful built-in content providers, along with a description of the type of data they're intended to store Browser CallLog Contacts MediaStore Settings Intended Data Browser bookmarks, browser history, etc. Missed calls, call details, etc. Contact details Media files such as audio, video and images Device settings and preferences 11 <uses-permission android:name="android.permission.read_contacts" /> 12 3
Get all call log Get settings 13 <uses-permission android:name="android.permission.read_call_log" /> 14 Steps to create your own ContentProvider Create your own Content Provider 1. First of all you need to create a class that extends the ContentProvider base class. 2. Second, you need to define your content provider URI address which will be used to access the content. 3. Next you will need to create your own database to keep the content. Usually, Android uses SQLite database and framework needs to override oncreate() method which will use SQLite Open Helper method to create or open the provider's database. When your application is launched, the oncreate() handler of each of its s is called on the main application thread. 4. Next you will have to implement queries to perform different database specific operations. 5. Finally register your in your activity file using <provider> tag. 15 16 4
Call-back methods Example: ContentProvider class Here is the list of methods which you need to override in Content Provider class to have your working: oncreate() This method is called when the provider is started. query() This method receives a request from a client. The result is returned as a Cursor object. insert()this method inserts a new record into the content provider. delete() This method deletes an existing record from the content provider. update() This method updates an existing record from the content provider. gettype() This method returns the MIME type of the data at the given URI. 17 18 Example: Registering Example: Client call ContentProvider 19 20 5