https://youtu.be/bc6wW-g6uss tacademy의 android content provider를 공부하다 찾아보게 된 자료이다. 

original source : https://stackoverflow.com/a/31600191

http://developer.android.com/guide/topics/providers/content-providers.html

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.

구조를 가진 data를 android에서 encapsulate해서도 다루는 경우가 있는데 이에 접근하는 경우 Uri 를 이용한다. content provider를 이용하는 경우 process간 data를 공유할수 있게 된다. 즉 다른 app간에 data를 공유할수 있게 된다는 이야기이다. 

https://developer.android.com/guide/topics/providers/content-providers.html?authuser=1

content provider 에 대한 google doc (길지 않고 개념 잡기 좋다)

Content providers

Content providers can help an application manage access to data stored by itself, stored by other apps, and provide a way to share data with other apps. 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. Implementing a content provider has many advantages. Most importantly you can configure a content provider to allow other applications to securely access and modify your app data as illustrated in figure 1.

image

Figure 1. Overview diagram of how content providers manage access to storage.

Use content providers if you plan to share data. If you don’t plan to share data, you may still use them because they provide a nice abstraction, but you don’t have to. This abstraction allows you to make modifications to your application data storage implementation without affecting other existing applications that rely on access to your data. In this scenario only your content provider is affected and not the applications that access it. For example, you might swap out a SQLite database for alternative storage as illustrated in figure 2.

Figure 2. Illustration of migrating content provider storage.

image

A number of other classes rely on the ContentProvider class:

If you are making use of any of these classes you also need to implement a content provider in your application. Note that when working with the sync adapter framework you can also create a stub content provider as an alternative. For more information about this topic, see Creating a stub content provider. In addition, you need your own content provider in the following cases:

  • You want to implement custom search suggestions in your application
  • You need to use a content provider to expose your application data to widgets
  • You want to copy and paste complex data or files from your application to other applications

The Android framework includes content providers that manage data such as audio, video, images, and personal contact information. You can see some of them listed in the reference documentation for the android.provider package. With some restrictions, these providers are accessible to any Android application.

A content provider can be used to manage access to a variety of data storage sources, including both structured data, such as a SQLite relational database, or unstructured data such as image files. For more information on the types of storage available on Android, see Storage options, as well as Designing data storage.

Advantages of content providers

Content providers offer granular control over the permissions for accessing data. You can choose to restrict access to a content provider from solely within your application, grant blanket permission to access data from other applications, or configure different permissions for reading and writing data. For more information on using content providers securely, see Security tips for storing data, as well as Content provider permissions.

You can use a content provider to abstract away the details for accessing different data sources in your application. For example, your application might store structured records in a SQLite database, as well as video and audio files. You can use a content provider to access all of this data, if you implement this development pattern in your application.

Also note that CursorLoader objects rely on content providers to run asynchronous queries and then return the results to the UI layer in your application. For more information on using a CursorLoader to load data in the background, see Running a query with a CursorLoader.

The following topics describe content providers in more detail:

Content provider basics

How to access and update data using an existing content provider.

Creating a content provider

How to design and implement your own content provider.

Calendar provider

How to access the Calendar provider that is part of the Android platform.

Contacts provider

How to access the Contacts provider that is part of the Android platform.

For sample code related to this page, refer to the Basic Sync Adapter sample application

original source : https://www.tutorialspoint.com/android/android_content_providers.htm

위 링크를 가면 실제 custom content provider를 만드는 과정과 실제 예제 코드가 있다.

A content provider component supplies data from one application to others on request. Such requests 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.

ContentProvider

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.

A content provider is implemented as a subclass of ContentProvider class and must implement a standard set of APIs that enable other applications to perform transactions.

public class My Application extends  ContentProvider {
}

Content URIs

To query a content provider, you specify the query string in the form of a URI which has following format −

<prefix>://<authority>/<data_type>/<id>

Here is the detail of various parts of the URI −

Create Content Provider

This involves number of simple steps to create your own content provider.

  • First of all you need to create a Content Provider class that extends the ContentProviderbaseclass.
  • Second, you need to define your content provider URI address which will be used to access the content.
  • 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 Content Providers is called on the main application thread.
  • Next you will have to implement Content Provider queries to perform different database specific operations.
  • Finally register your Content Provider in your activity file using <provider> tag.

Here is the list of methods which you need to override in Content Provider class to have your Content Provider working −

ContentProvider

  • 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.