Contentprovider组件功能实现

目录

一、实验要求:

二、实验内容:

1、请求方app:MyResolver

(1)在layout中进行界面布局,activitity_main.xml作用为显示MyResolver app界面,相关代码如下

(2)设计MainActivity.java在表格中添加一条数据

2、提供方app:MyContentProvider

(1)设计ContentProvider.java,实现crud操作

(2)MainActivity.java

(3)MyDAO.java实现数据库的创建以及数据的插入操作

(4)MyDBhelper.java创建表

三、结果展示:

四、源码地址


一、实验要求:

1、contentprovider是安卓四大组件之一,请使用其方法类进行数据获取;

2、请自建一个provider,然后在另一个app中使用resolver调用这个provider。

3、本次作业请启用新项目,理论上需要两个APP进行实验。

二、实验内容:

1、请求方app:MyResolver

相关主要文件如下:

(1)在layout中进行界面布局,activitity_main.xml作用为显示MyResolver app界面,相关代码如下

<?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="35sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
    <Button
        android:id="@+id/button"
        android:layout_width="157dp"
        android:layout_height="74dp"
        android:text="接收"
        android:textSize="35dp"
        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.224" />
 
</androidx.constraintlayout.widget.ConstraintLayout>

(2)设计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;

public class MainActivity extends AppCompatActivity {

    private Button button;
    private ContentResolver resolver;

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

        Uri uri=Uri.parse("content://hg.provider1/person");
        ContentResolver resolver=getContentResolver();
        ContentValues values=new ContentValues();
        values.put("name","hg");
        values.put("age",19);

        Integer.parseInt("19");

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resolver.insert(uri,values);
            }
        });
    }
}

2、提供方app:MyContentProvider

相关文件如下:

(1)设计ContentProvider.java,实现crud操作

相关代码:

package com.example.mycontentprovider;

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");
    }
//增  返回类型uri
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO: Implement this to handle requests to insert a new row.
        return myDAO.addvalue(uri,values);
    }
//创建
    @Override
    public boolean onCreate() {
        // TODO: Implement this to initialize your content provider on startup.
        Context context=getContext();
        myDAO=new MyDAO(context);
        return true;
    }
//查
    @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");
    }
}

(2)MainActivity.java

package com.example.mycontentprovider;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

        MyDAO myDAO=new MyDAO(this);
    }
}

(3)MyDAO.java实现数据库的创建以及数据的插入操作

package com.example.mycontentprovider;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;
//数据处理层,写一套在数据库上的增删改查
public class MyDAO {
    private SQLiteDatabase database;
    private SQLiteOpenHelper myopenhelper;
    private Context context;

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

    public MyDAO(Context context){
        this.context=context;
        myopenhelper=new MyDBhelper(context,"TKDB",null,1);
        database=myopenhelper.getReadableDatabase();

        database.execSQL("drop table if exists student");
        database.execSQL("create table student(id integer primary key autoincrement,"+" name varchar, age integer)");
    }

    public Uri addvalue(Uri uri,ContentValues values){
        long rowID=database.insert("person",null,values);

        if(rowID == -1){
            Log.d("DAO","数据插入失败");
            return  null;
        }
        else {
            Uri insertUri= ContentUris.withAppendedId(uri,rowID);
            Log.d("hg","ContentUris:"+insertUri.toString());
            context.getContentResolver().notifyChange(insertUri,null);
            return insertUri;
        }
    }

}

(4)MyDBhelper.java创建表

package com.example.mycontentprovider;

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, name, 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 sqLiteDatabase, int i, int i1) {

    }
}

三、结果展示:

点击insert进行插入

 

 

四、源码地址

Android_homework: as实验

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值