Android 中的内容提供程序及示例
什么是 Android 中的内容提供程序?
Android 中的内容提供程序是 Android 操作系统的一个组件,它使应用程序能够存储数据并与其他应用程序共享数据。内容提供程序在应用程序和 Android 数据源(如数据库、文件和 SharedPreferences)之间提供了一个接口。内容提供程序是 Android 应用程序开发的四个基本组件之一,另外三个是活动、服务和广播接收器。需要访问其他应用程序数据的应用程序使用内容提供程序。
内容提供程序通常用于存储和检索存储介质中的数据。此存储介质可以是数据库、文件或共享首选项。内容提供程序也可用于与其他应用程序共享数据,从而允许应用程序通信和传输数据。内容提供程序对于从远程源(例如 Web 服务器)存储和检索数据特别有用。
内容提供程序通过扩展 ContentProvider 类并定义标识内容提供程序的 URI(统一资源标识符)来创建。然后,此 URI 用于访问内容提供程序中的数据。内容提供程序通常使用 ContentResolver 类访问。ContentResolver 类提供用于查询和操作内容提供程序提供的数据的方法。
内容提供程序用于存储和检索诸如联系人、图像、视频、音乐和其他媒体之类的数据。例如,Android 联系人应用程序使用内容提供程序来存储和检索联系人数据。联系人应用程序提供了一个内容提供程序,其他应用程序可以使用该提供程序来访问联系人数据。类似地,Android 媒体存储应用程序使用内容提供程序来存储和检索媒体文件。
在本文中,我们将构建一个简单的应用程序,在该应用程序中,我们将从设备联系人应用程序中获取数据,并以列表视图的形式在我们的 Android 应用程序中显示该数据。在此应用程序中,我们使用联系人应用程序的内容在我们自己的应用程序中显示。
Android 中内容提供程序的实现
我们将创建一个简单的应用程序,在其中我们将显示一个列表视图。在这个列表视图中,我们将显示不同的编程语言,并为列表视图中的项目添加点击监听器。点击列表视图中的项目时,我们将向用户显示一条带有项目名称的吐司消息。我们将遵循分步指南在 Android 中实现两个数字的加法。
步骤 1 - 在 Android Studio 中创建新项目
导航到 Android Studio,如下面的屏幕截图所示。在下面的屏幕截图中,点击“新建项目”以创建一个新的 Android Studio 项目。
点击“新建项目”后,您将看到下面的屏幕。
在此屏幕中,我们只需选择“空活动”并点击“下一步”。点击“下一步”后,您将看到下面的屏幕。
在此屏幕中,我们只需指定项目名称。然后包名将自动生成。
注意 - 确保选择 Java 作为语言。
指定所有详细信息后,点击“完成”以创建一个新的 Android Studio 项目。
创建项目后,我们将看到 2 个打开的文件,即 activity_main.xml 和 MainActivity.java 文件。
步骤 3 - 使用 activity_main.xml。
导航到 activity_main.xml。如果此文件不可见,则要打开此文件。在左侧窗格中导航到 app>res>layout>activity_main.xml 以打开此文件。打开此文件后,将以下代码添加到其中。代码中添加了注释以详细了解。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/idRLLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--Creating a list view on below line-->
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
说明 - 在上面的代码中,我们创建了一个相对布局作为根布局,并在其中创建了我们的列表视图小部件以显示编程语言列表。对于列表视图小部件,我们将高度和宽度指定为 match_parent。并为其添加一个唯一的 ID,我们将使用它在列表视图中设置数据。
步骤 4:为列表视图的项目创建自定义布局文件
导航到 app>res>右键点击它>新建>布局资源文件,并将其命名为 contacts_list_item,并将以下代码添加到其中。代码中添加了注释以详细了解。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:orientation="vertical">
<!-- on below line creating a text view for contact name-->
<TextView
android:id="@+id/idTVContactName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="Contact Name"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold" />
<!-- on below line creating a text view for contact number -->
<TextView
android:id="@+id/idTVContactNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="Number"
android:textColor="@color/black"
android:textSize="16sp" /
</LinearLayout>
说明 - 在上面的代码中,我们创建了一个相对布局作为父布局。在此布局内,我们创建了两个文本视图。一个文本视图用于显示联系人的姓名,另一个文本视图用于在列表视图中显示该联系人的联系电话号码。
步骤 5:使用 MainActivity.java 文件。
导航到 MainActivity.java。如果此文件不可见,则要打开此文件。在左侧窗格中导航到 app>res>layout>MainActivity.java 以打开此文件。打开此文件后,将以下代码添加到其中。代码中添加了注释以详细了解。
package com.example.androidjavaapp;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// on below line creating a variable for list view.
private ListView contactsLV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// on below line we are initializing our variables.
contactsLV = findViewById(R.id.listView);
// create cursor and querying the data on below line
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
// on below line calling method to manage the cursor.
startManagingCursor(cursor);
// on below line getting the data from contacts
String[] data = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone._ID};
// on below line specifying id to which we have to set the data.
int[] to = {R.id.idTVContactNumber, R.id.idTVContactName};
// on below line creating and initializing simple cursor adapter and passing the layout file which we have to display.
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contacts_list_item, cursor, data, to);
// on below line setting adapter to our list view.
contactsLV.setAdapter(adapter);
// on below line setting choice mode for list view.
contactsLV.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
说明 - 在上面的代码中,首先我们为列表视图创建变量。在 onCreate 方法中,我们使用我们在 activity_main.xml 文件中给出的 ID 初始化列表视图变量。之后,我们创建并初始化游标变量,然后通过调用 startManagingCursor 方法设置游标的管理器。之后,我们创建一个字符串数组来获取联系电话号码和联系人姓名。然后,我们创建一个整数数组,在其中指定我们必须设置数据的文本视图的 ID。然后我们创建并初始化我们的适配器,并将数据传递给它。最后,我们将适配器设置为我们的列表视图。
添加上述代码后,我们只需点击顶栏中的绿色图标即可在移动设备上运行我们的应用程序。
注意 - 确保您已连接到您的真实设备或模拟器。
输出
结论
在上面的教程中,我们了解了什么是 Android 中的内容提供程序以及如何以不同的方式使用它们来获取其他应用程序中的数据。我们还构建了一个联系人解析器应用程序,该应用程序将读取设备联系人应用程序中的所有联系人。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP