新建一个provider,在另一个app中使用resolver调用这个provider实现数据获取

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


设计目标:

新建一个provider,然后在另一个app中使用resolver调用这个provider,实现跨app的数据获取,通过在resolver中写入对数据库的增删改查操作,使得在provider的app中可以直接显示这些操作的结果。

一、结果展示

activity_myresolver
在这里插入图片描述
Myresolver.java运行结果
在这里插入图片描述
provider结果展示,新增了(“王五”,19)
在这里插入图片描述

二、resolver中文件

1、activity_myresolver.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=".Myresolver">

    <TextView
        android:id="@+id/textView7"
        android:layout_width="192dp"
        android:layout_height="62dp"
        android:text="resolver"
        android:textSize="35sp"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.515"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.372" />

    <Button
        android:id="@+id/button11"
        android:layout_width="203dp"
        android:layout_height="67dp"
        android:text="insert"
        android:textSize="35sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.542"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView7"
        app:layout_constraintVertical_bias="0.464" />
</androidx.constraintlayout.widget.ConstraintLayout>

2、Myresolver.java

package com.example.myapplication;

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 Myresolver extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myresolver);
        ContentResolver resolver=getContentResolver();
        ContentValues values=new ContentValues();
        values.put("name","王五");
        values.put("age",19);

        Uri uri= Uri.parse("content://lp.provider1/student");
        Button button1=findViewById(R.id.button11);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                resolver.insert(uri,values);
            }
        });
    }
}

在AndroidManifest.xml中还需添加如下代码需要知道provider的类包

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

三、provider中的文件

1、MyContentProvider.java

package com.example.previder;

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 myDao1;
    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.
        return myDao1.Daoinsert(values);
    }

    @Override
    public boolean onCreate() {
        // TODO: Implement this to initialize your content provider on startup.
        Context context = getContext();
        myDao1 = 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");
    }
}

2、myDao.java

package com.example.previder;

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

public class myDao {
    private Context context;
    private SQLiteDatabase database;
    public myDao(Context context)
    {
        this.context=context;
        MyDBhelper dBhelper=new MyDBhelper(context,"lpDB",null,1);
        this.database=dBhelper.getWritableDatabase();
    }
    public Uri Daoinsert(ContentValues values)
    {
        long rowid=database.insert("student",null,values);
        Uri uri=Uri.parse("content://lp/.provider1");
        Log.d("lp","Dao");
        Uri inserturi = ContentUris.withAppendedId(uri,rowid);
        context.getContentResolver().notifyChange(inserturi,null);
        return inserturi;
    }
}

3、MyDBhelper.java

package com.example.previder;

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

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, "lpDB", factory, version);
    }

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

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

    }
}

在创建MyContentProvider.java文件时需要填入url,在AndroidManifest.xml中添加如下代码

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

总结

通过这次实验,我学到了如何使用contentprovider来实现数据获取。通过resolver的app使用ContentResolver实现对另一个app(provider)的操作,使得操作可以跨越app来实现,需要注意的是在resolver中的AndroidManifest.xml问文件下加入provider所拥有的类包。在创建MyContentProvider.java文件时需要注意url的填写。通过这次实验,知道了自己的不足之处要想有进步则需花时间认真完成。

仓库

https://gitee.com/liupeng321/gitee-liupeng
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值