読者です 読者をやめる 読者になる 読者になる

liguofeng29’s blog

個人勉強用ブログだっす。

AndroidのContentProvider - ContentProviderサンプル

実装の流れ

  1. ContentProviderサブクラス実装
  2. AndroidManifest.xml登録
  3. ContentResolverサブクラス実装
  4. 実行
package com.example.liguofeng.contentprovidersample;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class MyContentProvider extends ContentProvider {
    public MyContentProvider() {
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        System.out.println("delete()が呼ばれました");
        return -1;
    }

    @Override
    public String getType(Uri uri) {
        System.out.println("getType()が呼ばれました");
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        System.out.println("insert()が呼ばれました");
        return null;
    }

    @Override
    public boolean onCreate() {
        System.out.println("onCreate()が呼ばれました");
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        System.out.println("query()が呼ばれました");
        return null;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        System.out.println("update()が呼ばれました");
        return 1;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.liguofeng.contentprovidersample" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <provider
            android:name=".MyContentProvider"
            android:authorities="org.lee.providers"
            android:enabled="true"
            android:exported="true" >
        </provider>
    </application>

</manifest>
package com.example.liguofeng.contentresolver;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ContentResolver contentResolver;
    Uri uri = Uri.parse("content://org.lee.providers/");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button insertBtn = (Button) findViewById(R.id.insertBtn);
        Button updateBtn = (Button) findViewById(R.id.updateBtn);
        Button queryBtn = (Button) findViewById(R.id.queryBtn);
        Button deleteBtn = (Button) findViewById(R.id.deleteBtn);

        contentResolver = getContentResolver();
    }

    // ContentProviderのquery()
    public void query(View source) {
        Cursor cursor = contentResolver.query(this.uri, null, "query_where", null, null);
        Toast.makeText(this, "ContentProviderのCursorは" + cursor, Toast.LENGTH_LONG).show();
    }

    // ContentProviderのinsert()
    public void insert(View source) {
        // リクエストデータ
        ContentValues values = new ContentValues();
        values.put("key1", "value1");

        // 実行&戻り値Uri
        Uri newUri = contentResolver.insert(this.uri, values);

        Toast.makeText(this, "ContentProviderの挿入は" + newUri, Toast.LENGTH_LONG).show();
    }
    // ContentProviderのupdate()
    public void update(View source) {
        // リクエストデータ
        ContentValues values = new ContentValues();
        values.put("key1", "value1");

        // 実行&戻り値Uri
        int count = contentResolver.update(this.uri, values, "update_where", null);
        Toast.makeText(this, "ContentProviderの更新件数は" + count, Toast.LENGTH_LONG).show();
    }

    // ContentProviderのdelete()
    public void delete(View source) {
        int count = contentResolver.delete(this.uri, "delete_where", null);
        Toast.makeText(this, "ContentProviderの削除件数は" + count, Toast.LENGTH_LONG).show();
    }
}

f:id:liguofeng29:20151212230104g:plain