移动开发作业3

作业内容

1、contentprovider是安卓四大组件之一,请使用其方法类进行数据获取;
2、请自建一个provider,然后在另一个app中使用resolver调用这个provider。
3、本次作业请启用新项目,理论上需要两个APP进行实验。

ContentProvider 是 Android 的四大组件之一,有时候我们需要操作其他应用程序的一些数据,就会用到 ContentProvider,ContentProvider 本质上是一个中间者,真正 存储和操作数据 的数据源还是原来存储数据的方式,如数据库、文件或网络等。

ContentProvider 以相对安全的方式封装了数据并提供简易的处理机制和统一的访问接口供其他程序调用。它的底层采用了 Binder 机制来实现,ContentProvider 为应用间的数据交互提供了一个安全的环境:允许把自己的应用数据根据需求开放给 其他应用进行增删改查,而不用担心因为直接开放数据库权限而带来的安全问题。当然,ContentProvider不仅可以实现跨进程通信,也可实现进程内的通信。
 
用ContentResolver对数据进行CRUD操作

通过调用Content的 getContentResolver() 方法获取 ContentResolver对象实例,其实ContentResolver的作用类似于HttpClient,获取对象后就可以根据Uri对应用的数据进行CRUD操作了。

代码:
 

<?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>

  数据处理层,写在数据库上的增填操作,包括增、删、改、查、获得类型getType和创建onCreate,只需要在onCreate中传值并new一个MyDAO的方法即可。

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.
 
        //getContext().getContentResolver().insert(uri, values);
        return  myDAO.DAOinsert(values);
    }
 
    @Override
    public boolean onCreate() {
        // TODO: Implement this to initialize your content provider on startup.
        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");
    }
}

MyContentProvider  编写自己的ContenProvider

public class MyDAO {
    private Context context;
    private SQLiteDatabase database;
    public MyDAO(Context context){
        this.context=context;
 
        MyDBhelper dBhelper =new MyDBhelper(context,"lxDB",null,1);
        database=dBhelper.getReadableDatabase();
    }
 
    public Uri DAOinsert(ContentValues contentValues){
        long rowid=database.insert("student",null,contentValues);
 
        Uri uri=Uri.parse("content://lx.provider2/student");
        Uri inserturi=ContentUris.withAppendedId(uri,rowid);
        context.getContentResolver().notifyChange(inserturi,null);
        return inserturi;
    }
}

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);
    }
}

resovler的app主要代码

使用ContentResolver调用ContentProvider提供的接口,对ContentProvider中的数据进行添加、删除、修改和查询操作时。

可以使用Activity提供的getContentResolver()方法来获取ContentResolver对象。 ContentResolver 类提供了与ContentProvider类相同签名的四个方法:

    public Uri insert(Uri uri, ContentValues values)

    public int delete(Uri uri, String selection, String[] selectionArgs)。

    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)

    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

MainActivity.java
 

public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        ContentResolver resolver=getContentResolver();
 
        ContentValues values=new ContentValues();
        values.put("name","lx");
        values.put("age",20);
 
        Uri uri=Uri.parse("content://lx.provider2/student");
 
        Button button = findViewById(R.id.button);
 
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resolver.insert(uri,values);
            }
        });
    }
}

通过getContentResolver()方法获取resovler

并调用封装的增删改查方法操作数据库

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

activity_main.xml

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="resovler"
        android:textSize="45dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.526"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.363" />
 
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="insert"
        android:textSize="45dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.555"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.587" />

 总结

从字面上的意思来看,Content Providers是内容提供者,也就是数据的提供者,而数据的来源可以是SQLite数据库或者File存储等等方式。为了实现各个应用程序之间的数据共享,可以把应用程序的私有数据封装成ContentProviders,接着定义一个URI,向外提供统一的数据接口。其他的应用程序可以通过这个URI来访问指定的数据,然后实现各种操作,如添加(insert)、删除 (delete)、查询(query)、修改(update)。而不用去操作应用程序的底层私有数据。一般来说底层的私有数据是基于SQLite建立的。

为方便其它app与用户的应用程序的ContentProvider通信,通常要自定义一个公共的Uri:CONTENT_URI,其它app通过这一Uri,与ContentProvider交流。需要注意的是,当用户自己建立好ContentProvider之后,并不直接与之打交道,而是通过ContentResolver进行操作。

仓库

https://gitee.com/ywj_10710/add-contentprovide-component/issues

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值