安卓day34内容提供者

一、排坑

二、内容提供者

  • 应用的数据库是不允许其他应用访问的
  • 内容提供者的作用就是让别的应用访问到你的数据库
  • 自定义内容提供者,继承ContentProvider类,重写增删改查方法,在方法中写增删改查数据库的代码,

  • 在清单文件中定义内容提供者的标签,注意必须要有authorities属性,这是内容提供者的主机名,功能类似地址
  • 创建一个其他应用,访问自定义的内容提供者,实现对数据库的插入操作

 提供者

public class PersonProvider extends ContentProvider {

    private MyOpenHelper oh;
    SQLiteDatabase db;

    //创建uri匹配器对象
    static UriMatcher um = new UriMatcher(UriMatcher.NO_MATCH);
    //检测其他用户传入的uri与匹配器定义好的uri中,哪条匹配
    static {
        um.addURI("com.itheima.people", "person", 1);//content://com.itheima.people/person
        um.addURI("com.itheima.people", "teacher", 2);//content://com.itheima.people/teacher
        um.addURI("com.itheima.people", "person/#", 3);//content://com.itheima.people/person/4
    }
    
    //内容提供者创建时调用
    @Override
    public boolean onCreate() {
        oh = new MyOpenHelper(getContext());
        db = oh.getWritableDatabase();
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        Cursor cursor = null;
        if(um.match(uri) == 1){
            cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder, null);
        }
        else if(um.match(uri) == 2){
            cursor = db.query("teacher", projection, selection, selectionArgs, null, null, sortOrder, null);
        }
        else if(um.match(uri) == 3){
            //把uri末尾携带的数字取出来
            long id = ContentUris.parseId(uri);
            cursor = db.query("person", projection, "_id = ?", new String[]{id + ""}, null, null, sortOrder, null);
        }
        else{
            throw new IllegalArgumentException("uri又有问题哟亲么么哒");
        }
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        if(um.match(uri) == 1){
            return "vnd.android.cursor.dir/person";
        }
        else if(um.match(uri) == 3){
            return "vnd.android.cursor.item/person";
        }
        return null;
    }

    //此方法供其他应用调用,用于往people数据库里插数据
    //values:由其他应用传入,用于封装要插入的数据
    //uri:内容提供者的主机名,也就是地址
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        //使用uri匹配器匹配传入的uri
        if(um.match(uri) == 1){
            db.insert("person", null, values);
            
            //发送数据改变的通知
            //uri:通知发送到哪一个uri上,所有注册在这个uri上的内容观察者都可以收到这个通知
            getContext().getContentResolver().notifyChange(uri, null);
        }
        else if(um.match(uri) == 2){
            db.insert("teacher", null, values);
            
            getContext().getContentResolver().notifyChange(uri, null);
        }
        else{
            throw new IllegalArgumentException("uri有问题哟亲么么哒");
        }
        return uri;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int i = db.delete("person", selection, selectionArgs);
        return i;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        int i = db.update("person", values, selection, selectionArgs);
        return i;
    }

}
public class MyOpenHelper extends SQLiteOpenHelper {

    public MyOpenHelper(Context context) {
        super(context, "people.db", null, 2);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table person(_id integer primary key autoincrement, name varchar(10), money integer(20))");
        db.execSQL("create table teacher(_id integer primary key autoincrement, name varchar(10))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("create table teacher(_id integer primary key autoincrement, name varchar(10))");
    }

}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.index42.customcontentprovider">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider android:name=".provider.PersonProvider"
            android:authorities="com.itheima.people"
            android:exported="true"
            ></provider>
    </application>

</manifest>

猜你喜欢

转载自www.cnblogs.com/index42/p/10481898.html