ContentProvider for Android data storage

ContentProvider (content provider) is one of the four major components in Android. It is mainly used to share data externally, that is, to share the data in the application to other applications through the ContentProvider, and other applications can operate the data in the specified application through the ContentProvider. ContentProvider is divided into system and custom, the system is data such as contacts, pictures and so on.

Summary:
1. ContentProvider provides a unified interface for storing and reading data.
2. Using ContentProvider, applications can share data
. 3. Many data built in android are in the form of ContentProvider for developers to call (such as video, audio , pictures, contacts, etc.)

Uri Assistance:

1. Each ContentProvider has a public Uri, which is used to represent the data
provided by the ContentProvider. 2. The ContentProvider provided by Android is stored in the Android.provider package.

Android's ContentProvider URI has a fixed form: content://contract/people

Prefix: fixed as content: //
Authentication: contract The unique identifier
of the resource Path: people The specific resource type

 

A: Standard prefix, used to indicate that a Content Provider controls these data and cannot be changed; "content://"
B: URI identifier, which defines which Content Provider provides these data. For third-party applications, in order to guarantee the uniqueness of the URI identification, it must be a full, lowercase class name. This identification is described in the authorities attribute of the element: generally the package that defines the ContentProvider. The name of the class; "content://hx.android.text.myprovider"
C: "content://hx.android.text.myprovider/ "tablename" path, I don't know if it is a path or not. In layman's terms, it is the name of the table in the database you want to operate, or you can define it yourself. Remember to keep it consistent when you use it.

D: If the URI contains the ID of the record that needs to be obtained; then the data corresponding to the id will be returned, if there is no ID, it will return all; "content: //hx.Android .text.myprovider/tablename/#" # Indicates the data id

 

URIs are widely used in Android, not URLs. The URL identifies the physical location of the resource, which is equivalent to the path of the file, while the URI identifies the logical location of the resource and does not provide the specific location of the resource. For example, if the data in the phonebook is identified by URL, it may be a very complicated file structure, and once the storage path of the file is changed, the URL must also be changed. But if it is a URI, it can be identified by an easy-to-record logical address such as content://contract/people, and you don't need to care about the specific location of the file, even if the file location changes, you don't need to change it. Of course, this is all for the user. In other words, the mapping of the URI to the specific location in the background program still needs to be changed by the programmer.

Functions provided by ContentProvider:

1、query() 查询
2、insert() 插入
3、update() 更新
4、delete() 删除
5、getType() 得到数据类型
6、onCreate() 创建时的回调函数

实现ContentProvider的过程:

1、定义一个COTENT_URI常量
2、定义一个类,继承ContentProvider
3、实现query(),delete(),update(),insert(),onCreate(),getType()方法
4、在AndroidMainfest.xml中申明

下面是一个使用ContentProvider进行CRUD操作的例子。

IProivderMetaData.java

package com.liuzhichao.privoder;
import android.net.Uri;
import android.provider.BaseColumns;
public interface IProivderMetaData {
    // 定义外部访问的Authority
    public static final String AUTHORITY = "com.liuzhichao.bookprovider";
    // 数据库名称
    public static final String DB_NAME = "book.db";
    // 数据库版本
    public static final int VERSION = 1;
    public interface BookTableMetaData extends BaseColumns {
        // 表名
        public static final String TABLE_NAME = "book";
        // 外部程序访问本表的uri地址
        public static final Uri CONTENT_URI = Uri.parse("content://"
                + AUTHORITY + "/" + TABLE_NAME);
        // book表列名
        public static final String BOOK_ID = "_id";
        public static final String BOOK_NAME = "name";
        public static final String BOOL_PUBLISHER = "publisher";
        //默认排序
        public static final String SORT_ORDER="_id desc";
        //得到book表中的所有记录
        public static final String CONTENT_LIST="vnd.android.cursor.dir/vnd.bookprovider.book";
        //得到一个表信息
        public static final String CONTENT_ITEM="vnd.android.cursor.item/vnd.bookprovider.book";
    }
}

DBHelper.java

package com.liuzhichao.privoder;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBHelper extends SQLiteOpenHelper implements IProivderMetaData {
    private static final String TAG = "DBHelper";
    public DBHelper(Context context) {
        super(context, DB_NAME, null, VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        String TABLESQL = "create table if not exists "
                + BookTableMetaData.TABLE_NAME + " ("
                + BookTableMetaData.BOOK_ID + " integer primary key,"
                + BookTableMetaData.BOOK_NAME + " varchar,"
                + BookTableMetaData.BOOL_PUBLISHER + " varchar)";
        db.execSQL(TABLESQL);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + DB_NAME);
        onCreate(db);
    }
}

  BookContentProvider.java

package com.liuzhichao.privoder;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;
public class BookContentProvider extends ContentProvider {
    private static final String TAG="BookContentProvider";
    private static UriMatcher uriMatcher = null;
    private static final int BOOKS = 1;
    private static final int BOOK = 2;
    private DBHelper dbHelper;
    private SQLiteDatabase db;
    /**
     * 这部分就相当于为外部程序准备好一个所有地址匹配集合
     */
    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(IProivderMetaData.AUTHORITY,
                IProivderMetaData.BookTableMetaData.TABLE_NAME, BOOKS);
        uriMatcher.addURI(IProivderMetaData.AUTHORITY,
                IProivderMetaData.BookTableMetaData.TABLE_NAME+"/#", BOOK);
    }
    @Override
    public boolean onCreate() {
        dbHelper = new DBHelper(getContext());
        return (dbHelper == null) ? false : true;
    }
    // 取得数据的类型,此方法会在系统进行URI的MIME过滤时被调用。
    @Override
    public String getType(Uri uri) {
        switch (uriMatcher.match(uri)) {
        case BOOKS:
            return IProivderMetaData.BookTableMetaData.CONTENT_LIST;
        case BOOK:
            return IProivderMetaData.BookTableMetaData.CONTENT_ITEM;
        default:
            throw new IllegalArgumentException("This is a unKnow Uri"
                    + uri.toString());
        }
    }
    //增加
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        switch (uriMatcher.match(uri)) {
        case BOOKS:
            db = dbHelper.getWritableDatabase();//取得数据库操作实例 
            //执行添加,返回行号,如果主健字段是自增长的,那么行号会等于主键id 
            long rowId = db.insert(
                    IProivderMetaData.BookTableMetaData.TABLE_NAME,
                    IProivderMetaData.BookTableMetaData.BOOK_ID, values);
            Uri insertUri = Uri.withAppendedPath(uri, "/" + rowId);
            Log.i(TAG, "insertUri:"+insertUri.toString());
            //发出数据变化通知(book表的数据发生变化) 
            getContext().getContentResolver().notifyChange(uri, null);
            return insertUri;
        default:
             //不能识别uri 
            throw new IllegalArgumentException("This is a unKnow Uri"
                    + uri.toString());
        }
    }
    //查询
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        db = dbHelper.getReadableDatabase();
        switch (uriMatcher.match(uri)) {
        case BOOKS:
            return db
                    .query(IProivderMetaData.BookTableMetaData.TABLE_NAME,
                            projection, selection, selectionArgs, null, null,
                            sortOrder);
        case BOOK:
            long id = ContentUris.parseId(uri);  
            String where = "_id=" + id;  
            if (selection != null && !"".equals(selection)) {  
                where = selection + " and " + where;  
            }  
            return db.query(IProivderMetaData.BookTableMetaData.TABLE_NAME, projection, where, selectionArgs, null,  
                    null, sortOrder);  
        default:
            throw new IllegalArgumentException("This is a unKnow Uri"
                    + uri.toString());
        }
    }
    //删除
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        db = dbHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)) {
        case BOOKS:
            return db.delete(IProivderMetaData.BookTableMetaData.TABLE_NAME, selection, selectionArgs);
        case BOOK:
            long id = ContentUris.parseId(uri);
            String where = "_id=" + id;
            if (selection != null && !"".equals(selection)) {
                where = selection + " and " + where;
            }
            return db.delete(IProivderMetaData.BookTableMetaData.TABLE_NAME, selection, selectionArgs);
        default:
            throw new IllegalArgumentException("This is a unKnow Uri"
                    + uri.toString());
        }
    }
    //更新
    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        db = dbHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)) {
        case BOOKS:
            return db.update(IProivderMetaData.BookTableMetaData.TABLE_NAME,
                    values, null, null);
        case BOOK:
            long id = ContentUris.parseId(uri);
            String where = "_id=" + id;
            if (selection != null && !"".equals(selection)) {
                where = selection + " and " + where;
            }
            return db.update(IProivderMetaData.BookTableMetaData.TABLE_NAME,
                    values, selection, selectionArgs);
        default:
            throw new IllegalArgumentException("This is a unKnow Uri"
                    + uri.toString());
        }
    }
}

 ContentProviderDemoActivity.java

package com.liuzhichao.privoder;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ContentProviderDemoActivity extends Activity {
    private EditText et_bookId;
    private EditText et_bookName;
    private EditText et_bookPublisher;
    private Button btn_add;
    private Button btn_delete;
    private Button btn_update;
    private Button btn_query;
    private Button btn_showall;
    private Button btn_claer;
    ContentResolver contentResolver;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // 取得ContentResolver对象
        contentResolver = this.getContentResolver();
        findViews();
    }
    void findViews() {
        // 根据id获取组件
        et_bookId = (EditText) findViewById(R.id.book_id);
        et_bookName = (EditText) findViewById(R.id.book_name);
        et_bookPublisher = (EditText) findViewById(R.id.book_publisher);
        btn_add = (Button) findViewById(R.id.btn_add);
        btn_delete = (Button) findViewById(R.id.btn_delete);
        btn_update = (Button) findViewById(R.id.btn_update);
        btn_query = (Button) findViewById(R.id.btn_query);
        btn_showall = (Button) findViewById(R.id.btn_showall);
        btn_claer = (Button) findViewById(R.id.btn_claer);
        // 设置按钮单击事件处理
        btn_add.setOnClickListener(ClickListener);
        btn_delete.setOnClickListener(ClickListener);
        btn_update.setOnClickListener(ClickListener);
        btn_query.setOnClickListener(ClickListener);
        btn_showall.setOnClickListener(ClickListener);
        btn_claer.setOnClickListener(ClickListener);
    }
    // 按钮事件处理
    private View.OnClickListener ClickListener = new OnClickListener() {
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.btn_add:
                long rowid = btnAddHandler();
                Toast.makeText(getApplicationContext(), "数据增加成功,编号:" + rowid, 1)
                        .show();
                break;
            case R.id.btn_delete:
                long deleteRow=btnDeleteHandler();
                Toast.makeText(getApplicationContext(), "数据删除成功,删除了 "+deleteRow+" 行数据" , 1)
                .show();
                break;
            case R.id.btn_update:
                long updateRow=btnUpdateHandler();
                Toast.makeText(getApplicationContext(), "数据更新成功,更新了 "+updateRow+" 行数据" , 1)
                .show();
                break;
            case R.id.btn_query:
                btnQueryHandler();
                break;
            case R.id.btn_showall:
                startActivity(new Intent(getApplicationContext(), ShowAllBooksActivity.class));
                break;
            case R.id.btn_claer:
                et_bookId.setText("");
                et_bookName.setText("");
                et_bookPublisher.setText("");
                break;
            default:
                break;
            }
        }
    };
    /**
     * 增加图书
     * 
     * @return
     */
    private long btnAddHandler() {
        String bookname = et_bookName.getText().toString();
        String bookPubliser = et_bookPublisher.getText().toString();
        ContentValues values = new ContentValues();
        values.put(IProivderMetaData.BookTableMetaData.BOOK_NAME, bookname);
        values.put(IProivderMetaData.BookTableMetaData.BOOL_PUBLISHER,
                bookPubliser);
        Uri insertUri = contentResolver.insert(
                IProivderMetaData.BookTableMetaData.CONTENT_URI, values);
        return ContentUris.parseId(insertUri);
    }
    //删除
    private long btnDeleteHandler() {
        String bookId = et_bookId.getText().toString();
        long row=0;
        if (!"".equals(bookId) && bookId != null) {
            ContentValues values = new ContentValues();
            values.put(IProivderMetaData.BookTableMetaData.BOOK_ID, bookId);
            //根据id删除
            row=    contentResolver.delete(
                    Uri.withAppendedPath(IProivderMetaData.BookTableMetaData.CONTENT_URI, bookId), "_id = ?",
                    new String[] {bookId});
        } else {
            //删除所有
            row=    contentResolver.delete(
                    IProivderMetaData.BookTableMetaData.CONTENT_URI, null,
                    null);
        }
        return row;
    }
    //修改
    private long btnUpdateHandler() {
        String bookId = et_bookId.getText().toString();
        String bookname = et_bookName.getText().toString();
        String bookPubliser = et_bookPublisher.getText().toString();
        ContentValues values = new ContentValues();
        //values.put(IProivderMetaData.BookTableMetaData.BOOK_ID, bookId);
        values.put(IProivderMetaData.BookTableMetaData.BOOK_NAME, bookname);
        values.put(IProivderMetaData.BookTableMetaData.BOOL_PUBLISHER,
                bookPubliser);
        long rowid=0;
        if ("".equals(bookId) || bookId == null) {
            //更新所有
            rowid=contentResolver.update(
                    IProivderMetaData.BookTableMetaData.CONTENT_URI, values,
                    null, null);
        }else{
            //根据Id更新
            rowid=contentResolver.update(
                    Uri.withAppendedPath(IProivderMetaData.BookTableMetaData.CONTENT_URI, bookId), values,
                    "_id = ? ", new String[]{bookid});
        }
        return rowid;
    }
    //查询(根据Id查询)
    private void btnQueryHandler() {
        String bookId = et_bookId.getText().toString();
        ContentValues values = new ContentValues();
        values.put(IProivderMetaData.BookTableMetaData.BOOK_ID, bookId);
        Cursor cursor = contentResolver.query(
                IProivderMetaData.BookTableMetaData.CONTENT_URI, null,
                " _id = ?", new String[] { bookId }, null);
        if (cursor.moveToNext()) {
            et_bookName
                    .setText(cursor
                            .getString(
                                    cursor.getColumnIndex(IProivderMetaData.BookTableMetaData.BOOK_NAME))
                            .toString());
            et_bookPublisher
                    .setText(cursor
                            .getString(
                                    cursor.getColumnIndex(IProivderMetaData.BookTableMetaData.BOOL_PUBLISHER))
                            .toString());
        }
    }
}

 ShowAllBooksActivity.java

package com.liuzhichao.privoder;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;
public class ShowAllBooksActivity extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(
                IProivderMetaData.BookTableMetaData.CONTENT_URI, null, null,
                null, null);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.list, cursor, new String[] {
                        IProivderMetaData.BookTableMetaData.BOOK_ID,
                        IProivderMetaData.BookTableMetaData.BOOK_NAME,
                        IProivderMetaData.BookTableMetaData.BOOL_PUBLISHER },
                new int[] { R.id.book_id, R.id.book_name, R.id.book_publisher });
        setListAdapter(adapter);
    }
}

 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/bookid" />
    <EditText
        android:id="@+id/book_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/bookname" />
    <EditText
        android:id="@+id/book_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/bookpublisher" />
    <EditText
        android:id="@+id/book_publisher"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text" />
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <Button
        android:id="@+id/btn_add"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/btn_add" />
    <Button
        android:id="@+id/btn_delete"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/btn_delete" />
    <Button
        android:id="@+id/btn_update"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/btn_update" />
    <Button
        android:id="@+id/btn_query"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/btn_query" />
       </LinearLayout>
 <Button
        android:id="@+id/btn_showall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_showall" />
 <Button
        android:id="@+id/btn_claer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_clear" />
</LinearLayout>

 activity_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal">

    <TextView
        android:id="@+id/book_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/book_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/book_publisher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
</LinearLayout>

Don't forget to register the provider

<provider android:name=".BookContentProvider" android:authorities="com.liuzhichao.bookprovider"/>

 running result:

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326430373&siteId=291194637