欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ContentProvider内容提供者和ContentResolver内容访问者

程序员文章站 2024-02-11 14:06:25
...


提供者的demo代码:

MyProvider.java

package com.zking.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import com.zking.db.DBHelper;
import android.database.sqlite.SQLiteDatabase;
/**
 * Created by Administrator on 2017/7/18 0018.
 */

public class MyProvider extends ContentProvider{
    @Override
    public boolean onCreate() {
        Log.i("test","onCreate");
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        Log.i("test","query");
        //把数据查询出来
        DBHelper dbHelper=new DBHelper(getContext(),"G160618.db",null,2);
        SQLiteDatabase sqLiteDatabase=dbHelper.getReadableDatabase();
        return sqLiteDatabase.rawQuery("select * from login",null);
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        Log.i("test","getType");
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        Log.i("test","insert");
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        Log.i("test","delete");
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        Log.i("test","update");
        return 0;
    }
}



DBHelper.java

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by Administrator on 2017/7/18 0018.
 */

public class DBHelper extends SQLiteOpenHelper{
    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        Log.i("test","构造方法");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i("test","创建表");
        db.execSQL("create table login(uid integer primary key autoincrement,uname,upass)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i("test","升级");
    }
}

配置

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

    <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:authorities="com.example.g160628_16_widget.zhongzi"
            android:name="com.zking.provider.MyProvider"
            android:exported="true"
            ></provider>
    </application>

</manifest>


访问者的demo代码:

在这里我就直接在后台测试了,所以我们需要改动的只有一个类

MainActivity类:


package com.example.g160628_25_contentresolver;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取第16次课的数据
        //获取内容访问者
        ContentResolver cr=getContentResolver();
        //必须加content协议
        Uri uri=Uri.parse("content://com.example.g160628_16_widget.zhongzi");
        Cursor cursor=cr.query(uri,null,null,null,null);
        while (cursor.moveToNext()){
            String uname=cursor.getString(cursor.getColumnIndex("uname"));
            Log.i("test",uname);
        }

    }
}