Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2028096
  • 博文数量: 413
  • 博客积分: 10926
  • 博客等级: 上将
  • 技术积分: 3862
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-09 18:14
文章分类

全部博文(413)

文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(6)

2011年(138)

2010年(85)

2009年(42)

2008年(46)

2007年(26)

2006年(59)

分类: LINUX

2010-02-24 16:07:33

  1. Document
    • Content Providers
      http://developer.android.com/guide/topics/providers/content-providers.html
    • Sample
      • Define Cursor
        http://www.netmite.com/android/mydroid/cupcake/frameworks/base/core/java/android/database/sqlite/SQLiteCursor.java
      • Build a Custom Content Provider
        http://www.devx.com/wireless/Article/41133/1763/page/2
      • ContentProvider Example

      • Weather Data Provider & Resolver
        - WeatherDataProvider.java (Customize cursor with MatrixCursor)
        http://developer.android.com/resources/samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherDataProvider.html
        - WeatherWidgetProvider.java
        http://developer.android.com/resources/samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherWidgetProvider.html
      • NotePad
        http://developer.android.com/guide/tutorials/notepad/index.html
      • xxx
    • xxx
  2. Basic
    1. What's content provider
      Content providers store and retrieve data and make it accessible to all applications (Other applications can query or even modify the data (if the content provider allows it)). They're the only way to share data across applications. You can store the data in the file system, an SQLite database, on the web, or any other persistent storage location your application can access.

      If you only want to share data between some specific applicatios developed by your corporation, you can arrange these applications to share the same Linux user ID, in which case they are able to access each other's files. To conserve system resources, applications with the same user ID can also arrange to run in the same Linux process and share the same VM (the applications must also be signed with the same certificate). (Refer to: http://developer.android.com/guide/topics/fundamentals.html)

      The system instantiates all ContentProvider objects when a request is sent from ContentResolver; you never need to do it on your own. In fact, you never deal directly with ContentProvider objects at all. Typically, there's just a single instance of each type of ContentProvider. But it can communicate with multiple ContentResolver objects in different applications and processes. The interaction between processes is handled by the ContentResolver and ContentProvider classes.
    2. How to interact with content provider
      You can use the ContentResolver's methods to interact with whatever content providers you're interested in, including that of application itself.
      When a query() is initiated, the Android system identifies the content provider that's the target of the query and makes sure that it is up and running. The system instantiates all ContentProvider objects; you never need to do it on your own. In fact, you never deal directly with ContentProvider objects at all.
      Typically, there's just a single instance of each type of ContentProvider. But it can communicate with multiple ContentResolver objects in different applications and processes.
    3. What is data model
      ontent providers expose their data as a simple table on a database model.
    4. xxx
  3. Classes
    • ContentProvider
      Provide contents to other applications. Content Provider is the unique way to share data between multiple applications.
      • openOutputStream
        The Uri is returned from insert(xxx), so you need to compose the proper Uri in ContentProvider.insert.
      • openInputStream
        need return the proper ParcelFileDescriptor from ContentProvider.openFile/openAssetFile???
      • xxx
    • ContentResolver
      get throuth Context.getContentResolver();

      Provide applications to acess or modify the content model, or register content observer.

      Addition: the methods (such as query, insert, update, delete etc) of ContentResolver is in sync mode; If you need to call this method in async mode, juse use AsyncQueryHandler.
    • ContentObserver
      Receives callbacks for changes to content.
      • register/unregister via: ContentResolver.registerContentObserver/ContentResolver.unregisterContentObserver
      • be notified via: ContentResolver.notifyChange
    • ContentValues
      Store a set of values (kay/value map) that ContentResolver can process.
    • Cursor
      Results returned by ContentResolver.query().
    • Uri & UriMatcher
    • AsyncQueryHandler
      A helper class to help make handling asynchronous ContentResolver queries easier.
    • xxx
  4. FAQ
    1. Handle binary data
      • small data
        Write: put (String key, byte[] value)
        Read: Cursor.getBlob()
      • large data (such as images and documents)
        On content provider, implement openAssetFile/openFile
        Read/Write: openAssetFileDescriptor/openFileDescriptor/openInputStream/openOutputStream/openTypedAssetFileDescriptor
      • xxx
    2. Set ContentProvider's permission
      • In ContentProvider application


        android:name="xxxxx"
        android:authorities="xxxx"
        android:readPermission="com.xxx.provider.permission.READ"
        android:writePermission="com.xxx.provider.permission.WRITE">



      • In ContentResolver application



        com.xxx.provider.permission.READ" />
        com.xxx.provider.permission.WRITE" />
        com.xxx.provider.permission.READ/>
        com.xxx.provider.permission.WRITE" />
      • xxx
    3. If you need to implement your own Cursor, you need to inherit AbstractedWindowedCursor, more details, please refer to SQLiteCursor.java. Or MatrixCursor
  5. xxx








Contents URIs and column related to mms/sms/mms-sms/contacts/telephony are defined in java.android.provider.Telephony.java

Android content provider基础与使用


如果你想实现不同应用之间的数据共享,就不得不用content provider了。

在 Android中,content provider是一个特殊的存储数据的类型,它提供了一套标准的接口用来获取以及操作数据。并且,android自身也提供了几个现成的content provider:Contacts, Browser, CallLog, Settings, MediaStore.

应用可以通过一个唯一的ContentResolver interface来使用具体的某个content provider。

ContentResolver cr = getContentResolver();

然 后你就可以用ContentResolver提供的方法来使用你需要的content provider了。其中contentResolver提供的方法包括query(),insert(),update()等。要使用这些方法,还会涉 及到一个东西,那就是Uri。你可以将它理解成一个string形式的contentProvider的完全路径,它的形式 为:/// /

例如:

content://browser/bookmarks

content://contacts/people

content://contacts/people/3

下面结合一个实例来看我们如何使用一个已有的content provider,给例子展示了如何从已有的电话本中读出联系人信息:


package com.android.cp;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.util.Log;
import android.widget.Toast;

public class ContentProviderTest extends Activity {
    private final String TAG = "ContentProviderTest";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG,"enter onCreate");
        setContentView(R.layout.main);
        createCP();
    }
    
    public void createCP()
    {
        ContentResolver cr = getContentResolver();
        
        //Cursor cur = managedQuery(People.CONTENT_URI, null, null, null, null);

        Cursor cur = cr.query(People.CONTENT_URI, null, null, null, null);
        
        getColumnData(cur);

    }
    
    private void getColumnData(Cursor cur){
        if (cur.moveToFirst()) {

            String name;
            String phoneNumber;
            int nameColumn = cur.getColumnIndex(People.NAME);
            int phoneColumn = cur.getColumnIndex(People.NUMBER);
        
            do {
                // Get the field values

                name = cur.getString(nameColumn);
                phoneNumber = cur.getString(phoneColumn);
              
                Log.i(TAG, "name="+name);
                DisplayToast(name+" "+phoneNumber);

            } while (cur.moveToNext());

        }
    }
    
    public void DisplayToast(String s)
    {
    Toast.makeText(this,
    s,
    Toast.LENGTH_LONG).show();
    }

}

需要注意的是,你需要在你的Manifest文件中加上


android:name="android.permission.READ_CONTACTS">

否则,程序无法成功运行。
阅读(1991) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~