移动开发作业三

实验内容:

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

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

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

实验代码:

TextView和Button控件代码如下

<?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="INSERT"
        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" />

MainActivity.java

package com.example.resolver;

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 {

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

        ContentValues values=new ContentValues();
        values.put("name","sy");
        values.put("age",20);
      "content://"+AUTHORITY+"/person"  /person数据库表名
        Uri uri=Uri.parse("content://hf.provider1/person");
        Integer.parseInt("20");
        Button button1=findViewById(R.id.button);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resolver.insert(uri,values);
            }
        });
    }
}

AndroidManifest.xml 

注册provider

<provider
            android:authorities="com.example.homework.MyProvider"
            android:name=".MyProvider"
            android:enabled="true"
            android:exported="true"/>

通过ContentResolver接口提供的方法类访问app1中MyProvider提供的数据

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    
    private Button btn_add;
    private Button btn_delete;
    private Button btn_query;
    private Button btn_update;
    
    private static final String TAG = "MainActivity";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        bangID();
    }
 
    private void bangID() {
        btn_add = findViewById(R.id.btn_insert);
        btn_delete = findViewById(R.id.btn_delete);
        btn_query = findViewById(R.id.btn_query);
        btn_update = findViewById(R.id.btn_update);
        
        btn_add.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        btn_query.setOnClickListener(this);
        btn_update.setOnClickListener(this);
 
 
    }
 
    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btn_insert:
                Uri uri1 = Uri.parse("content://com.example.homework.MyProvider/people");
                ContentValues values = new ContentValues();
                values.put("name","sy");
                getContentResolver().insert(uri1,values);
                Log.d("app2","Insert success!");
                break;
            case R.id.btn_delete:
                Uri uri2 = Uri.parse("content://com.example.homework.MyProvider/people");
                getContentResolver().delete(uri2,"name=?",new String[]{"yjj"});
                Log.d("app2","Delete success!");
                break;
            case R.id.btn_query:
                Uri uri = Uri.parse("content://com.example.homework.MyProvider/people");
                Cursor cursor = getContentResolver().query(uri,null,null,null,null);
                cursor.moveToFirst();
                do{
                    @SuppressLint("Range") String name2 = cursor.getString(cursor.getColumnIndex("name"));
                    Log.e(TAG, "onClick: "+name2 );
 
                }while(cursor.moveToNext());
                cursor.close();
                Log.d("app2","Query success!");
                break;
            case R.id.btn_update:
                Uri uri3 =Uri.parse("content://com.example.homework.MyProvider/people");
                ContentValues values1 = new ContentValues();
                values1.put("name","sy");
                getContentResolver().update(uri3,values1,"name=?",new String[]{""});
                Log.d("app2","Update success!");
                break;
            default:
        }
 
    }
}

结果截图:

 

代码仓库地址:as_work: android studio

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值