Android Content Provider Tutorial--安卓内容提供者系列1--内容提供者介绍

We noticed that there's not a lot of information out there on content providers, so we thought we'd share the following excerpt from NewCircle's Android Bootcamp course. Though this tutorial was designed for our in-person training experience, there's a lot of great info here that we wanted to get out to everyone who might not be able to join us in class. Enjoy!

我们注意到网上并没有太多有关ContentProviders的资料信息,所以我们觉得应该把NewCircleAndroid训练营的一部分这方面课程拿出来给大家分享一下。虽然本教程是专为我们的现场培训制作的,但这里确实有很多有用的信息,我们想把它也分享给那些无法来我们现场听课的朋友们。现在请尽情享受这份福利吧!

Objectives(学习目标)

An Android app by default cannot access another app’s data. This is the cornerstone of Android security, the principle of sandboxing. But often, you do want an app to share some data with others. Content providers are such an interface to data so that the data can be mashed together across apps. In this module, you will learn how to design and develop content providers. Topics covered include:

在安卓系统默认的情况下,一个app是不能访问另一个app的数据的。这种基于沙盒的原则,是保护安卓系统安全的基石。但确实经常会有这样的需求:你想让一个app能分享一些数据给其他app。Content providers就是这样一个数据接口,能使数据在不同app之间共享。在这一部分学习中,您将学会如何设计和开发Content providers。下面是这部分学习的主题:

  • Why content providers: an overview
  • 为什么要用Content providers:概述
  • Using existing content providers: creating client code that can read and modify the data managed by a content provider
  • 使用已有的content providers:创建客户端代码,使其能够读取和修改由content providers管理的数据
  • Creating a content provider: implementing a basic content provider to expose structured data to other applications
  • 创建一个content provider:实现一个最基本的content provider,把自己app的结构化数据暴露给其他app
  • Intelligent data loading: using loaders to retrieve a Cursor from a content provider without blocking your application’s main thread
  • 智能数据加载:在不阻塞主线程的情况下,使用加载器从content provider中检索游标

Content Provider OverviewContent Provider 概述)

 Example of Content Provider
  • Content Providers share content with applications across application boundaries.
  • Content Providers在app之间共享数据内容。
  • Examples of built-in Content Providers in Android OS are:
  • 在Android系统内置的Content Providers的例子有:
    • Contacts
    • MediaStore
    • Bookmarks
    • Settings and more.

Typical Usage of Content ProvidersContent Providers的典型用法)

Here’s an example of how Contacts system may work in Android. It comprises of two applications: one responsible for the UI and the other responsible for the data.

这里有一个例子可以说明联系人模块在Android系统中是如何工作的。它包括两个应用程序:一个负责用户界面,另一个负责数据。

Typical Usage of Content Providers

Content Provider LifecycleContent Provider的生命周期

Content Provider Lifecycle
  • Content provider is initiated first time it is used via a call to onCreate().
  • Content Provider的初始化是在通过调用onCreate()方法第一次使用它的时候
  • There is no callback for cleaning up after the provider.
  • 用完之后Content Provider没有用来清理的回调方法
  • When modifying the data (insert/update/delete), open/close database atomically.
  • 当修改数据(如插入/更新/删除)时,将会自动打开/关闭数据库。
  • When reading the data, leave database open or else the data will get garbage collected.
  • 在读取数据时,要让数据库打开,否则数据会被当做垃圾回收掉。

Content Provider TemplateContent Provider模版

This is a minimal shell implementation of a content provider. We’ll discuss the details of the implementation later.

这是content provider的一个小小实现例子。后面我们再讨论实现的具体细节。

ProviderDemo.java
package com.marakana.android.lifecycle;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import android.provider.Settings.System;

public class ProviderDemo extends ContentProvider {
     
     
  static final String TAG = "ProviderDemo";

  static final String AUTHORITY = "content://com.marakana.android.lifecycle.providerdemo";
  public static final Uri CONTENT_URI = Uri.parse(AUTHORITY);
  static final String SINGLE_RECORD_MIME_TYPE = "vnd.android.cursor.item/vnd.marakana.android.lifecycle.status";
  static final String MULTIPLE_RECORDS_MIME_TYPE = "vnd.android.cursor.dir/vnd.marakana.android.lifecycle.status";

  @Override
  public boolean onCreate() {
     
     
    Log.d(TAG, "onCreate");
    return true;
  }

  @Override
  public String getType(Uri uri) {
     
     
    String ret = getContext().getContentResolver().getType(System.CONTENT_URI);
    Log.d(TAG, "getType returning: " + ret);
    return ret;
  }

  @Override
  public Uri insert(Uri uri, ContentValues values) {
     
     
    Log.d(TAG, "insert uri: " + uri.toString());
    return null;
  }

  @Override
  public int update(Uri uri, ContentValues values, String selection,
      String[] selectionArgs) {
     
     
    Log.d(TAG, "update uri: " + uri.toString());
    return 0;
  }

  @Override
  public int delete(Uri uri, String selection, String[] selectionArgs) {
     
     
    Log.d(TAG, "delete uri: " + uri.toString());
    return 0;
  }

  @Override
  public Cursor query(Uri uri, String[] projection, String selection,
      String[] selectionArgs, String sortOrder) {
     
     
    Log.d(TAG, "query with uri: " + uri.toString());
    return null;
  }

}

Content Provider Callbacks(Content Provider 的回调方法)

onCreate()

Used to initialize this content provider. This method runs on UI thread, so should be quick. Good place to instantiate database helper, if using database.

这是用于初始化content provider的方法。该方法在UI线程上运行,所以会很快。你如果要使用数据库的话,那这个方法是实例化数据库的helper的好地方。


getType()

Returns the mime time for the given uri. Typically, this MIME type will either be something like vnd.android.cursor.item/vnd.marakana.android.lifecycle.status for a single item or vnd.android.cursor.dir/vnd.marakana.android.lifecycle.status for multiple items.

对于给定的uri返回对应的mime类型。通常,这个MIME类型既可以是针对单个项目的类似vnd.android.cursor.item / vnd.marakana.android.lifecycle,也可以是针对多个项目的类似vnd.android.cursor.dir / vnd.marakana.android.lifecycle。


insert()

Inserts the values into the provider returning uri that points to the newly inserted record.

向ContentProvider中插入新值,将会返回一个指向新插入记录的uri。


update()

Updates records(s) specified by either the uri or selection/selectionArgs combo. Returns number of records affected.

更新那些uri或selection/selectionArgs组合指定的数据记录。返回的是已修改记录的数目。


delete()

Deletes records(s) specified by either the uri or selection/selectionArgs combo. Returns number of records affected.

删除那些uri或selection/selectionArgs组合指定的数据记录。返回的是已删除记录的数目。


query()

Queries the provider for the record(s) specified by either uri or`selection`/selectionArgs/grouping/having combo.

查询那些uri或selection/selectionArgs组合指定的数据记录。

Registering Content Provider(注册Content Provider

Registering in Android Manifest file
在manifest 清单文件中注册一下

...<provider
     android:name=".ProviderDemo"
     android:authorities="com.marakana.android.lifecycle.providerdemo" />...

The authority of this provider must match the uri authority that this provider is responding to.

Content Provider的权限必须和其响应的uri的权限相匹配。


下一篇链接:Android Content Provider Tutorial--安卓内容提供者系列2--内容提供者用法

猜你喜欢

转载自blog.csdn.net/woshiwangbiao/article/details/52574624