contentprovider实验+SQLite数据库的实现

【实验目的】

  1. 掌握如何在Android 开发中使用 SQLite 数据库
  2. 熟悉设计数据库表结构的方法与步骤
  3. 理解contentprovider的使用方法及流程,理解ContentProvider、Resolver、Uri、Urimatcher等的原理。

【实验内容】

1、实现contentprovider和contentresolver通过uri的调用;

  2、实现contentprovider对数据库sqlite的功能:增、删、改、查;

  3、数据库的表结构自行设计;

【实验要求】

1、配置SQLite 数据库的运行环境

2、熟悉contentprovider对数据库sqlite增、删、改、查的具体操作和流程

3、充分理解SQLite数据库的使用原理和方式

【实验设计】

src/main/java/com/example/contentprovider/:包含所有的Java类文件。

MainActivity.java:应用的主活动,包含按钮点击事件处理。

ContentActivity.java:内容提供者活动,但未实现具体功能。

SQLActivity.java:数据库操作活动,处理增删改查操作。

PersonDBOpenHelper.java:数据库帮助类,用于创建和管理数据库。

PersonProvider.java:内容提供者类,用于管理数据的增删改查。

src/main/res/:包含资源文件。

layout/:包含布局文件。

activity_main.xml:主活动的布局。

contentlayout.xml:内容活动的布局。

sqllayout.xml:SQL活动的布局。

mipmap/:包含应用图标资源。

values/:包含字符串和其他资源值。

AndroidManifest.xml:定义应用的配置,包括活动、内容提供者等组件。

关键组件:

活动(Activity):

MainActivity:定义了跳转到其他活动的按钮和逻辑。

ContentActivity:未实现具体功能。

SQLActivity:定义了与数据库交互的按钮和逻辑。

内容提供者(ContentProvider):

PersonProvider:提供对info表的增删改查操作。

数据库帮助类(SQLiteOpenHelper):

PersonDBOpenHelper:用于创建和管理person.db数据库。

布局文件(XML):

activity_main.xml、contentlayout.xml、sqllayout.xml:定义了用户界面。

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.ContentProvider" tools:targetApi="31">
        <activity android:name=".MainActivity" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".ContentActivity"/>
        <activity android:name=".SQLActivity"/>
        <provider android:authorities="com.example.contentprovider.PersonProvider" android:name=".PersonProvider" android:exported="false"/>
    </application>
</manifest>

ContentActivity.java

package com.example.contentprovider;

public class ContentActivity {
}

MainActivity.java

package com.example.contentprovider

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    fun toOne(view: View?) {
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.READ_CONTACTS
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_CONTACTS), 1)
            Toast.makeText(this, "未允许读取通讯录权限!", Toast.LENGTH_SHORT).show()
        }
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.READ_CONTACTS
            ) == PackageManager.PERMISSION_GRANTED
        ) {
            val intent = Intent(
                this@MainActivity,
                ContentActivity::class.java
            )
            startActivity(intent)
        }
    }

    fun toTwo(view: View?) {
        val intent = Intent(
            this@MainActivity,
            SQLActivity::class.java
        )
        startActivity(intent)
    }
}

PersonDBOpenHelper.java

package com.example.contentprovider;

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

public class PersonDBOpenHelper extends SQLiteOpenHelper {

    public PersonDBOpenHelper(Context context) {
        super(context,"person.db",null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table info(_id integer primary key autoincrement,name varchar(50),phone varchar(20),UNIQUE(phone))");
    }

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

    }
}

PersonProvider.java

package com.example.contentprovider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class PersonProvider extends ContentProvider {

    private static UriMatcher mUriMatcher = new UriMatcher(-1);

    private static final int SUCCESS = 1;

    private PersonDBOpenHelper helper;

    static {
        mUriMatcher.addURI("com.example.contentprovider.PersonProvider","info",SUCCESS);
    }

    @Override
    public boolean onCreate() {
        helper = new PersonDBOpenHelper(getContext());
        return false;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        int code = mUriMatcher.match(uri);
        if (code == SUCCESS) {
            SQLiteDatabase db = helper.getReadableDatabase();
            long rowId = db.insert("info",null,values);
            if (rowId>0) {
                Uri insertedUri = ContentUris.withAppendedId(uri,rowId);

                getContext().getContentResolver().notifyChange(insertedUri,null);
                return insertedUri;
            }
            db.close();
            return uri;
        }else {
            try {
                throw new IllegalAccessException("插入失败,路径不正确!");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1) {
        int code = mUriMatcher.match(uri);
        if (code == SUCCESS) {
            SQLiteDatabase db = helper.getReadableDatabase();
            return db.query("info",strings,s,strings1,null,null,s1);
        }else {
            try {
                throw new IllegalAccessException("查询失败,路径不正确!");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) {

        int code = mUriMatcher.match(uri);
        if (code == SUCCESS) {
            SQLiteDatabase db = helper.getReadableDatabase();
            int count = db.update("info",contentValues,s,strings);
            if (count>0) {
                getContext().getContentResolver().notifyChange(uri,null);
            }
            db.close();
            return count;
        }else {
            try {
                throw new IllegalAccessException("更新失败,路径不正确!");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String s, @Nullable String[] strings) {
        int code = mUriMatcher.match(uri);
        if (code == SUCCESS) {
            SQLiteDatabase db = helper.getReadableDatabase();
            int count = db.delete("info",s,strings);
            if (count>0) {
                getContext().getContentResolver().notifyChange(uri,null);
            }
            db.close();
            return count;
        }else {
            try {
                throw new IllegalAccessException("删除失败,路径不正确!");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return code;
    }
}

SQLActivity.java

package com.example.contentprovider;
import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.*;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.*;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SQLActivity extends Activity {
    private final Uri uri = Uri.parse("content://com.example.contentprovider.PersonProvider/info");
    private ContentValues values;
    private ContentResolver resolver;
    private EditText et_id;
    private EditText et_name;
    private EditText et_phone;
    private EditText et_queryAll;

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

        et_id = (EditText) findViewById(R.id.editText1);
        et_name = (EditText) findViewById(R.id.editText2);
        et_phone = (EditText) findViewById(R.id.editText3);
        et_queryAll = (EditText) findViewById(R.id.eT);
    }

    public String getAllPhoneNumbers(String lookUp_Key){
        StringBuilder allPhoneNo = new StringBuilder();
        String[] proj2 = {ContactsContract.CommonDataKinds.Phone.NUMBER};
        String selection = ContactsContract.Data.LOOKUP_KEY+"=?";
        String[] selectionArgs = {lookUp_Key};
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,proj2,selection,selectionArgs,null);
        while(cursor.moveToNext()){
            allPhoneNo.append(cursor.getString(0)).append(" ");
        }
        return allPhoneNo.toString();
    }

    public void btnCreate(View view) {

        PersonDBOpenHelper helper = new PersonDBOpenHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        resolver = getContentResolver();
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},1);
            Toast.makeText(this,"未允许读取通讯录权限!",Toast.LENGTH_SHORT).show();
        }
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)== PackageManager.PERMISSION_GRANTED)
        {
            Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,null,null,null);
            String name = ContactsContract.Contacts.DISPLAY_NAME;
            String key = ContactsContract.Contacts.LOOKUP_KEY;
            int displayNameIndex = cursor.getColumnIndex(name);
            int KeyIndex = cursor.getColumnIndex(key);
            for (cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()) {
                String displayName = cursor.getString(displayNameIndex);
                String Key = cursor.getString(KeyIndex);
                String displayPhone = getAllPhoneNumbers(Key);
                values = new ContentValues();
                values.put("name",displayName);
                values.put("phone",displayPhone);
                db.insert("info",null,values);
            }
            cursor.close();
            db.close();
            btnQueryAll(view);
            Toast.makeText(this,"插入成功!",Toast.LENGTH_SHORT).show();

        }
    }

    public void btnAdd(View view) {
        resolver = getContentResolver();
        values = new ContentValues();
        if (et_name.length() != 0 && et_phone.length() != 0){
            values.put("name",et_name.getText().toString());
            values.put("phone",et_phone.getText().toString());
            Uri newUri = resolver.insert(uri,values);
            Toast.makeText(this,"增加成功",Toast.LENGTH_SHORT).show();
            btnQueryAll(view);
        } else {
            et_name.setHint("请在此输入需增加的姓名");
            et_phone.setHint("请在此输入需增加的号码");
        }
    }

    public void btnQuery(View view) {
        resolver = getContentResolver();
        if (et_name.length() != 0){
            Cursor cursor = resolver.query(uri,new String[]{"_id","name","phone"},"name=?",new String[]{et_name.getText().toString()},null);
            if (cursor.getCount() != 0){
                cursor.moveToFirst();
                et_id.setText(cursor.getString(0));
                et_name.setText(cursor.getString(1));
                et_phone.setText(cursor.getString(2));
                cursor.close();
            }
            else {
                Toast.makeText(this,"未查询到结果!",Toast.LENGTH_SHORT).show();
            }
        }
        else {
            et_name.setHint("请在此输入需查询的姓名");
            et_phone.setHint("号码");
        }
    }

    public void btnQueryAll(View view) {
        resolver = getContentResolver();

        List<Map<String,String>> data = new ArrayList<Map<String,String>>();
        Cursor cursor = resolver.query(uri,new String[]{"_id","name","phone"},null,null,null);
        while (cursor.moveToNext()){
            Map<String,String> map = new HashMap<String,String>();
            map.put("_id",cursor.getString(0));
            map.put("name",cursor.getString(1));
            map.put("phone",cursor.getString(2));
            data.add(map);
        }
        cursor.close();
        et_queryAll.setText(new String(data.toString()));
    }

    public void btnUpdate(View view) {
        resolver = getContentResolver();
        values = new ContentValues();
        if (et_name.length() != 0 && et_phone.length() != 0){
            values.put("phone",et_phone.getText().toString());
            int updateCount = resolver.update(uri,values,"name=?",new String[]{et_name.getText().toString()});
            Toast.makeText(this,"成功更新了" + updateCount + "条记录",Toast.LENGTH_SHORT).show();
            btnQueryAll(view);
        } else {
            et_name.setHint("请在此输入需修改的姓名");
            et_phone.setHint("请在此输入修改后的号码");
        }
    }

    public void btnDelete(View view) {
        resolver = getContentResolver();
        if (et_name.length() != 0){
            int deleteCount = resolver.delete(uri,"name=?",new String[]{et_name.getText().toString()});
            Toast.makeText(this,"成功删除了" + deleteCount + "条记录",Toast.LENGTH_SHORT).show();
            btnQueryAll(view);
        } else {
            et_name.setHint("请在此输入需删除的姓名");
            et_phone.setHint("号码");
        }
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
    <Button android:id="@+id/toOne" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/toOne" android:onClick="toOne"/>
    <Button android:id="@+id/toTwo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/toTwo" android:onClick="toTwo"/>
</LinearLayout>

contentlayout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".ContentActivity">
    <TextView android:id="@+id/hint" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hint" android:textSize="30sp"/>
    <TextView android:id="@+id/callName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp"/>
</LinearLayout>

sqllayout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".SQLActivity">
<EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:hint="@string/id"/>
<EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:hint="@string/name"/>
<EditText android:id="@+id/editText3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:hint="@string/phone"/>
<Button android:id="@+id/add" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/add" android:onClick="btnAdd"/>
<Button android:id="@+id/delete" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/delete" android:onClick="btnDelete"/>
<Button android:id="@+id/update" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/update" android:onClick="btnUpdate"/>
<Button android:id="@+id/query" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/query" android:onClick="btnQuery"/>
<Button android:id="@+id/addContent" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/addContent" android:onClick="btnCreate"/>
<Button android:id="@+id/queryAll" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/queryAll" android:onClick="btnQueryAll"/>
<EditText android:id="@+id/eT" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="15dp" android:layout_weight="1" android:gravity="start|top" android:inputType="textMultiLine"/>
</LinearLayout>

【实验分析或心得】

第一部分:Android组件和生命周期

Activity:

MainActivity展示了如何处理按钮点击事件并跳转到其他活动。

活动的生命周期方法(如onCreate)用于初始化界面和事件监听器。

ContentProvider:

PersonProvider展示了如何使用内容提供者管理数据的增删改查操作。

内容提供者可以被多个应用共享和访问。

SQLiteOpenHelper:

PersonDBOpenHelper展示了如何使用SQLiteOpenHelper创建和管理数据库。

数据库帮助类负责数据库的创建、升级和管理。

第二部分:数据存储和用户界面

数据存储:

使用SQLite数据库存储数据,通过内容提供者进行数据操作。

PersonProvider和PersonDBOpenHelper共同管理数据的存储和访问。

用户界面:

使用LinearLayout和Button等UI组件构建用户界面。

布局文件(XML)定义了用户界面的结构和样式。

权限管理:

在AndroidManifest.xml中声明了READ_CONTACTS权限,确保应用有权限访问联系人数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值