Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 1 year has passed since last update.

ContentProvider

Posted at

ContentProvider を使用すると、アプリケーションはデータの取得、格納、共有が容易になり、他のアプリケーションとデータを共有することができます。主に次のような用途で使用されます
DateBaseは単独で存在するが、そのDataBaseの除法をほかのアプリに提供するのがContentProvider。

データを提供するアプリ

①マニフェストにContentProvider を登録する。

 <!-- 自分のContentProviderにアクセスさせるための権限を宣言する -->
<permission android:name="com.example.contentproviertest.READ_DATA" />
<application>
    <provider
    android:name=".UserContentProvider"
    android:authorities="com.example.myapp"
    android:exported="false" />
</application>

②ContentProvider を実装

class UserContentProvider : ContentProvider() {

    companion object {
        // ContentProvider の識別用 URI
        val CONTENT_URI: Uri = Uri.parse("content://com.example.myapp/users")
    }

    // SQLite データベースなど、データのバックエンドを管理する変数などを宣言

    // データベースの初期化や必要な初期化処理を行う
    override fun onCreate(): Boolean {
        return true
    }

    override fun query(
        uri: Uri,
        projection: Array<String>?,
        selection: String?,
        selectionArgs: Array<String>?,
        sortOrder: String?
    ): Cursor? {
        // データのクエリ処理を実装し、結果を Cursor で返す
        // ここでは仮の実装として null を返しています
        return null
    }

    override fun insert(uri: Uri, values: ContentValues?): Uri? {
        // データの挿入処理を実装し、挿入されたデータの URI を返す
        // ここでは仮の実装として null を返しています
        return null
    }

    // データの更新処理を実装し、更新された行数を返す
    override fun update(
        uri: Uri,
        values: ContentValues?,
        selection: String?,
        selectionArgs: Array<String>?
    ): Int {
        return 0
    }

    // データの削除処理を実装し、削除された行数を返す
    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int {
        return 0
    }

    // MIME タイプを返す
    override fun getType(uri: Uri): String? {
        return null
    }
}

uri (Uri):

クエリ対象のデータを識別するための URI。
URI(Uniform Resource Identifier)は、識別子の一般的な形式で、リソースを一意に識別するために使用されます。
例: content://com.example.myapp/users
scheme(スキーム):
リソースにアクセスするための手段を示すもので、http、https、content などがあります。例えば、httpスキームはウェブ上のリソースにアクセスするために使われますが、contentスキームはContentProviderを通じてアプリ内のデータにアクセスするために使われます。
authority(オーソリティ):
リソースの所有者や管理者を示すもので、通常はホスト名やデータベース名などが含まれます。ContentProviderの場合、通常はアプリのパッケージ名が含まれ、アプリが提供するデータに対する一意の名前空間を作ります。
path(パス):
リソースの具体的な場所を示すもので、ツリー構造を持つことがあります。ContentProviderでは、データベース内のテーブルやエントリに対応するパスが指定されます。


projection (Array?):

取得するデータの列(カラム)を指定するための文字列配列。
null の場合、全ての列が対象になります。
例: arrayOf("column1", "column2")
selection (String?):

SQL の WHERE 句に相当する条件を指定する文字列。
null の場合、条件が指定されていないことを意味します。
例: "name = ?"
selectionArgs (Array?):

selection 文字列内のプレースホルダに対応する値を指定する文字列配列。
null の場合、プレースホルダは使用されません。
例: arrayOf("John")
sortOrder (String?):

クエリ結果のソート順を指定する文字列。
null の場合、デフォルトのソート順が適用されます。
例: "name ASC"

データを読み取るアプリ

①マニフェストに使用するContentProvider を登録する。

<!-- ContentProvider側の <permission> と同じ名前を <uses-permission> で宣言 -->
    <uses-permission android:name="com.example.contentproviertest.READ_DATA" />

    <queries>
        <!-- ContentProvider側の <provider> の authorities と同じものを指定 -->
        <provider android:authorities="com.example.myapp" />
    </queries>

②ContentResolverを使用してContentProvider にアクセス

val contentResolver = context.contentResolver
val uri = UserContentProvider.CONTENT_URI

// ユーザー情報をクエリ
val cursor: Cursor? = contentResolver.query(uri, null, null, null, null)

参考(https://qiita.com/niusounds/items/da178316d1d8800aaf7c)

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?