示例
本示例将向您说明如何创建自己的ContentProvider(内容提供者)。因此,让我们按照以下步骤进行操作,类似于创建
Hello World 例子时遵循的步骤-
- 您将使用Android StudioIDE创建一个Android应用程序,并在com.example.helloworld包下将其命名为HelloWrold,Activity为空。
- 修改主activity文件MainActivity.java以添加两个新方法onClickAddName()和onClickRetrieveStudents()。
- 在com.example.helloworld包下创建一个名为StudentsProvider.java的新Java文件, 以定义您的实际提供程序和相关方法。
- 使用<provider ... />标记在AndroidManifest.xml文件中注册内容提供者
- 修改res/layout/activity_main.xml文件的默认内容,以包括一个小的GUI图形界面以添加学生记录。
- 运行该应用程序以启动Android模拟器并验证在该应用程序中所做更改的结果。
以下是修改后的主activity文件src/com.jc2182.helloworld/MainActivity.java的内容。该文件可以包括每个基本生命周期方法。我们添加了两个新方法onClickAddName()和onClickRetrieveStudents()来处理用户与应用程序的交互。
package com.jc2182.helloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClickAddName(View view) {
// 添加一条学生新记录
ContentValues values = new ContentValues();
values.put(StudentsProvider.NAME, ((EditText)findViewById(R.id.editText2)).getText().toString());
values.put(StudentsProvider.GRADE, ((EditText)findViewById(R.id.editText3)).getText().toString());
Uri uri = getContentResolver().insert( StudentsProvider.CONTENT_URI, values);
Toast.makeText(getBaseContext(), uri.toString(), Toast.LENGTH_LONG).show();
}
public void onClickRetrieveStudents(View view) {
// 检索学生记录
String URL = "content://com.jc2182.helloworld.StudentsProvider";
Uri students = Uri.parse(URL);
Cursor c = managedQuery(students, null, null, null, "name");
if (c.moveToFirst()) {
do{
Toast.makeText(this,
c.getString(c.getColumnIndex(StudentsProvider._ID)) +
", " + c.getString(c.getColumnIndex( StudentsProvider.NAME)) +
", " + c.getString(c.getColumnIndex( StudentsProvider.GRADE)),
Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}
}
}
在com.jc2182.helloworld包下创建新文件StudentsProvider.java ,以下是src/com.jc2182.helloworld/StudentsProvider.java的内容 -
package com.jc2182.helloworld;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import java.util.HashMap;
public class StudentsProvider extends ContentProvider {
static final String PROVIDER_NAME = "com.jc2182.helloworld.StudentsProvider";
static final String URL = "content://" + PROVIDER_NAME + "/students";
static final Uri CONTENT_URI = Uri.parse(URL);
static final String _ID = "_id";
static final String NAME = "name";
static final String GRADE = "grade";
private static HashMap<string, string> STUDENTS_PROJECTION_MAP;
static final int STUDENTS = 1;
static final int STUDENT_ID = 2;
static final UriMatcher uriMatcher;
static{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "students", STUDENTS);
uriMatcher.addURI(PROVIDER_NAME, "students/#", STUDENT_ID);
}
/**
* 数据库特定的常量声明
*/
private SQLiteDatabase db;
static final String DATABASE_NAME = "College";
static final String STUDENTS_TABLE_NAME = "students";
static final int DATABASE_VERSION = 1;
static final String CREATE_DB_TABLE =
" CREATE TABLE " + STUDENTS_TABLE_NAME +
" (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
" name TEXT NOT NULL, " +
" grade TEXT NOT NULL);";
/**
* 实际创建和管理提供程序的基础数据存储库的Helper类。
*/
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DB_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + STUDENTS_TABLE_NAME);
onCreate(db);
}
}
@Override
public boolean onCreate() {
Context context = getContext();
DatabaseHelper dbHelper = new DatabaseHelper(context);
/**
* 创建一个可写数据库,如果该数据库尚不存在,它将触发其创建。
*/
db = dbHelper.getWritableDatabase();
return (db == null) ? false : true;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
/**
* 添加新的学生记录
*/
long rowID = db.insert( STUDENTS_TABLE_NAME, "", values);
/**
* 如果记录添加成功
*/
if (rowID > 0) {
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(_uri, null);
return _uri;
}
throw new SQLException("无法将记录添加到 " + uri);
}
@Override
public Cursor query(Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(STUDENTS_TABLE_NAME);
switch (uriMatcher.match(uri)) {
case STUDENTS:
qb.setProjectionMap(STUDENTS_PROJECTION_MAP);
break;
case STUDENT_ID:
qb.appendWhere( _ID + "=" + uri.getPathSegments().get(1));
break;
default:
}
if (sortOrder == null || sortOrder == ""){
/**
* 默认情况下,按学生姓名排序
*/
sortOrder = NAME;
}
Cursor c = qb.query(db, projection, selection,
selectionArgs,null, null, sortOrder);
/**
* 注册以观看内容URI的更改
*/
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)){
case STUDENTS:
count = db.delete(STUDENTS_TABLE_NAME, selection, selectionArgs);
break;
case STUDENT_ID:
String id = uri.getPathSegments().get(1);
count = db.delete( STUDENTS_TABLE_NAME, _ID + " = " + id +
(!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public int update(Uri uri, ContentValues values,
String selection, String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)) {
case STUDENTS:
count = db.update(STUDENTS_TABLE_NAME, values, selection, selectionArgs);
break;
case STUDENT_ID:
count = db.update(STUDENTS_TABLE_NAME, values,
_ID + " = " + uri.getPathSegments().get(1) +
(!TextUtils.isEmpty(selection) ? " AND (" +selection + ')' : ""), selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri );
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)){
/**
*获取所有学生记录
*/
case STUDENTS:
return "vnd.android.cursor.dir/vnd.example.students";
/**
* 找一个指定的学生
*/
case STUDENT_ID:
return "vnd.android.cursor.item/vnd.example.students";
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
}
以下将修改AndroidManifest.xml文件的内容 。在这里,我们添加了<provider ... />标签以包括我们的内容提供者:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jc2182.helloworld">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:name="StudentsProvider"
android:authorities="com.jc2182.helloworld.StudentsProvider"/>
</application>
</manifest>
以下是res/layout/activity_main.xml文件的内容 -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内容提供者例子"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蝴蝶教程"
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/logo"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText3"
android:layout_alignStart="@+id/textView2"
android:layout_alignLeft="@+id/textView2"
android:layout_alignEnd="@+id/textView2"
android:layout_alignRight="@+id/textView2"
android:layout_centerVertical="true"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="37dp"
android:layout_marginEnd="-3dp"
android:layout_marginRight="-3dp"
android:onClick="onClickAddName"
android:text="添加学生" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageButton"
android:layout_alignEnd="@+id/imageButton"
android:layout_alignRight="@+id/imageButton"
android:layout_marginTop="24dp"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textView1"
android:layout_alignLeft="@+id/textView1"
android:layout_alignTop="@+id/editText"
android:layout_alignEnd="@+id/textView1"
android:layout_alignRight="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginStart="-25dp"
android:layout_marginLeft="-25dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="25dp"
android:layout_marginRight="25dp"
android:hint="姓名"
android:textColorHint="@android:color/holo_blue_light" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText"
android:layout_alignStart="@+id/editText2"
android:layout_alignLeft="@+id/editText2"
android:layout_alignEnd="@+id/editText2"
android:layout_alignRight="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:hint="年级"
android:textColorHint="@android:color/holo_blue_bright" />
<Button
android:id="@+id/button"
android:layout_width="117dp"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_alignStart="@+id/button2"
android:layout_alignLeft="@+id/button2"
android:layout_alignEnd="@+id/editText3"
android:layout_alignRight="@+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="58dp"
android:layout_marginEnd="19dp"
android:layout_marginRight="19dp"
android:onClick="onClickRetrieveStudents"
android:text="检索学生" />
</RelativeLayout>
让我们尝试运行修改后的Hello World!我们刚刚修改的应用程序。我假设您在进行环境设置时已创建了AVD。要从Android Studio运行该应用,请打开您项目的活动文件之一,然后点击Android StudioRun图标工具栏中的“运行” 图标。Android Studio将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将显示在“模拟器”窗口下面-
现在启动内容提供者,让我们单击“添加学生”按钮,这将保存一条记录到数据库,如下所示:
点击“检索学生”按钮,将保存所有记录的学生打印出来,如下所示: