contentprovider的使用

设计目标:

  • contentprovider是安卓四大组件之一,请使用其方法类进行数据获取;
  • 请自建一个provider,然后在另一个app中使用resolver调用这个provider;
  • 请使用两个APP进行实验。

功能说明:

  • 在Myresolver项目中实现对Myprovider项目的数据库的修改

代码解析:

Myprovider项目:

MyContentProvider.java:

package com.example.myprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

public class MyContentProvider extends ContentProvider {
    private MyDAO myDAO;
    public MyContentProvider() {
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO: Implement this to handle requests to insert a new row.
        //throw new UnsupportedOperationException("Not yet implemented");
        return myDAO.plfInsert(values);
    }

    @Override
    public boolean onCreate() {
        Context context = getContext();
        myDAO = new MyDAO(context);
        return false;
    }


    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        // TODO: Implement this to handle query requests from clients.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

MyDAO.java:

package com.example.myprovider;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class MyDAO<database> {

    private Context context;
    private SQLiteDatabase database;

    public MyDAO(Context context)
    {
        this.context=context;
        MyDBHelper myDBHelper=new MyDBHelper(context,"plfDB",null,1);
        database=myDBHelper.getWritableDatabase();
    }


    public Uri plfInsert(ContentValues values){

        Uri uri=Uri.parse("content://plf/.provider1");

        long rowId=database.insert("student",null,values);
        Uri inserturi = ContentUris.withAppendedId(uri,rowId);

        context.getContentResolver().notifyChange(inserturi,null);
        return inserturi;

    }


}

MyDBHelper.java:

package com.example.myprovider;

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

import androidx.annotation.Nullable;

public class MyDBHelper extends SQLiteOpenHelper {
    public MyDBHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, "plfDB", factory, version);

    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table student (id integer primary key autoincrement,name varchar(20),age integer)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

AndroidManifest.xml

在AndroidManifest.xml文件中添加如下代码:

<provider
            android:name=".MyContentProvider"
            android:authorities="plf.provider1"
            android:enabled="true"
            android:exported="true"></provider>

Myresolver项目:

MainActivity.java:

package com.example.myresolver;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private Button button;

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

        button=findViewById(R.id.button);
        TextView textView=findViewById(R.id.textView);

        ContentResolver contentResolver=getContentResolver();

        Uri uri=Uri.parse("content://plf.provider1/student");

        ContentValues contentValues=new ContentValues();
        contentValues.put("name","plf");
        contentValues.put("age",21);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contentResolver.insert(uri,contentValues);
            }
        });
    }

}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="resolver"
        android:textSize="48sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.475"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.483" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.168" />

</androidx.constraintlayout.widget.ConstraintLayout>

页面设计:
在这里插入图片描述

AndroidManifest.xml

在AndroidManifest.xml文件中添加如下代码:

    <queries>
        <package android:name="com.example.myprovider"></package>
    </queries>

运行展示截图:

在点击Myresolver项目的button时,在Myprovider项目的数据库中会添加一组数据:
在这里插入图片描述


源码仓库地址:

Myprovider gitee源码仓库地址

Myresolver gitee源码仓库地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值