[转]Android下ContentProvider 学习总计

本文转自:http://hi.baidu.com/yangduoliver/blog/item/a95efcf2577f1b06b07ec5c6.html

1、ContentProvider的基本概念

     1> ContentProvider为存储和读取数据提供了合并的接口

     2> 使用ContentProvider,应用程序可以实现数据共享

     3>
android内置的不在少数数据都是利用ContentProvider形式,供开发者调用的(如视频,音频,图片,通讯录等)

2、Uri

     1>
每一个ContentProvider都兼备一个公共的Uri,这些Uri用于表示这些ContentProvider提供的数目

     2>
Android所提供的ContentProvider都存放在andriod.provider这个包里面

3、ContentProvider提供的函数

     1> query() 查询

     2> insert() 插入

     3> update() 更新

     4> delete() 删除

   5> getType() 得到数据类型

     6> onCreate() 创立时的回调函数

4、实现ContentProvider的过程

    1> 定义一个COTENT_URI常量

    2> 定义一个类,继承ContentProvider

    3>
实现query(),delete(),update(),insert(),onCreate(),getType()方法

    4> 在AndroidMainfest.xml中申明

下面以六个实例(一个是ContentProvider所在的应用,另一个是应用ContentProvider的利用),表达什么利用ContentProvider

5、ContentProvider所在的施用

1>定义一个类,里面定义一些常量

 

package com.yyl.android;

import android.net.Uri;
import android.provider.BaseColumns;

public class MyUsers {
   
    public static final String AUTHORITY =
“com.yyl.android.MyContentProvider”;
   
    //BaseColumn类中曾经包含了_id字段
    public static final class User implements BaseColumns
    {
        //定义Uri
        public static final Uri CONTENT_URI = Uri.parse(“content://” +
AUTHORITY);
        //定义数据表列
        public static final String USER_NAME = “USER_NAME”;
    }

}

2>定义一个延续ContentProvider的子类,实现其模式

package com.yyl.android;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

/*
 *
MyContentProvider继承ContentProvider类,实现其insert,update,delete,getType,onCreate等方法
 */
public class MyContentProvider extends ContentProvider {

    //定义一个SQLiteDatabase变量
    private SQLiteDatabase sqlDB;
    //定义一个DatabaseHelper变量
    private DatabaseHelper dbHelper;
    //数据库名
    private static final String DATABASE_NAME = “Users.db”;
    //数据库版本
    private static final int DATABASE_VERSION = 1;
    //表名
    private static final String TABLE_NAME = “User”;
    //标签
    private static final String TAG = “MyContentProvider”;

    /*
     * 定义一个内部类
     *
     * 这么些里面类继承SQLiteOpenHelper类,重写其方法
     */
    public static class DatabaseHelper extends SQLiteOpenHelper {

        //构造方法
        public DatabaseHelper(Context context) {
            //父类构造方法
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

       
//当第一遍创建数据库的时候调用该形式,可以为数据库扩充部分表,和着手化一些数量
        @Override
        public void onCreate(SQLiteDatabase db) {
            //在数据库里生成一张表
            db.execSQL(“Create table “
                    + TABLE_NAME
                    + “( _id INTEGER PRIMARY KEY AUTOINCREMENT,
USER_NAME TEXT);”);
        }

       
//当更新数据库版本的时候,调用该办法。可以去除,修改表的一对音信
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
            db.execSQL(“DROP TABLE IF EXISTS ” + TABLE_NAME);
            onCreate(db);

        }

    }

   
//这是一个回调函数,当生成所在类的对象时,这一个法子被调用,创造一个数据库
    @Override
    public boolean onCreate() {
        dbHelper = new DatabaseHelper(getContext());
        return (dbHelper == null) ? false : true;
    }

    //查询
    @Override
    public Cursor query(Uri uri, String[] projection, String
selection,
            String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        qb.setTables(TABLE_NAME);
        Cursor c = qb.query(db, projection, selection, null, null,
null,
                sortOrder);
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    }

    //取得类型
    @Override
    public String getType(Uri uri) {
        return null;
    }

    //插入数据
    @Override
    public Uri insert(Uri uri, ContentValues contentvalues) {
        sqlDB = dbHelper.getWritableDatabase();
        long rowId = sqlDB.insert(TABLE_NAME, “”, contentvalues);
        if (rowId > 0) {
            Uri rowUri = ContentUris.appendId(
                    MyUsers.User.CONTENT_URI.buildUpon(),
rowId).build();
            getContext().getContentResolver().notifyChange(rowUri,
null);
            return rowUri;
        }
        throw new SQLException(“Failed to insert row into” + uri);
    }

    //删除数据
    @Override
    public int delete(Uri uri, String selection, String[]
selectionArgs) {
        return 0;
    }

    //更新数据
    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        return 0;
    }

}

3>定义一个默认加载的Activity,里面对ContentProvider举办数据加载

package com.yyl;

import com.yyl.android.MyUsers;

import android.app.Activity;
import android.content.ContentValues;
sqlite,import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

public class MyContentDemo extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        insertRecord(“MyUser”);
        insertRecord(“YangYuLin”);
        displayRecords();
    }

    private void insertRecord(String userName) {
        ContentValues values = new ContentValues();
        values.put(MyUsers.User.USER_NAME, userName);
        getContentResolver().insert(MyUsers.User.CONTENT_URI,
values);
    }

    private void displayRecords() {
        String columns[] = new String[] { MyUsers.User._ID,
                MyUsers.User.USER_NAME };
        Uri myUri = MyUsers.User.CONTENT_URI;
        Cursor cur = managedQuery(myUri, columns, null, null, null);
        if (cur.moveToFirst()) {
            String id = null;
            String userName = null;
            do {
                id =
cur.getString(cur.getColumnIndex(MyUsers.User._ID));
                userName = cur.getString(cur
                        .getColumnIndex(MyUsers.User.USER_NAME));
                Toast.makeText(this, id + ” ” + userName,
Toast.LENGTH_LONG)
                        .show();
            } while (cur.moveToNext());
        }
    }
}

 

4>在AndroidMainfest.xml里注册

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest
xmlns:android=”http://schemas.android.com/apk/res/android
      package=”com.yyl”
      android:versionCode=”1″
      android:versionName=”1.0″>
    
    <application android:icon=”@drawable/icon”
android:label=”@string/app_name”>
        <provider android:name=”.android.MyContentProvider”
android:authorities=”com.yyl.android.MyContentProvider” />
        <activity android:name=”.MyContentDemo”
                  android:label=”@string/app_name”>
            <intent-filter>
                <action android:name=”android.intent.action.MAIN”
/>
                <category
android:name=”android.intent.category.LAUNCHER” />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion=”7″ />
   
</manifest>

 

6、做另一个App.使用方面ContentProvider提供的数目

package yyl.client;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class CPClientActitity extends Activity {
  
    public static final String AUTHORITY =
“com.yyl.android.MyContentProvider”;   
    private Button insertButton = null;
    //访问ContentProvider的Uri
    Uri CONTENT_URI = Uri.parse(“content://” + AUTHORITY);
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        //得到ContentProvider对于表的有着数据,以游标格式保存
        Cursor c = managedQuery(CONTENT_URI,new
String[]{“_id”,”USER_NAME”},null,null,null);
       
        //循环打印ContentProvider的多寡
        if(c.moveToFirst())
        {
            String _id = null;
            String user_name = null;
           
            do
            {
                //得到_id列,USER_NAME列
                _id = c.getString(c.getColumnIndex(“_id”));
                user_name =
c.getString(c.getColumnIndex(“USER_NAME”));
               
                System.out.println(“_id = ” + _id +”, user_name = ” +
user_name);
               
            }while(c.moveToNext());
        }
       
        //依据Id拿到控件对象
        insertButton = (Button)findViewById(R.id.Insert);
        //给按钮绑定事件监听器
        insertButton.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View v) {
               
                //得到EditText输入的多少
                String username =
((EditText)findViewById(R.id.userName)).getText().toString();
                //生成一个ContentResolver对象
                ContentResolver cr = getContentResolver();
                //生成一个ContentValues对象
                ContentValues values = new ContentValues();
                //将EditText输入的值,保存到ContentValues对象中
                values.put(“USER_NAME”, username);               
                //插入数据
                cr.insert(CONTENT_URI, values);
            }
        });
    }
}

 

网站地图xml地图