- 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
- Basic
- 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.
- 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.
- What is data model
ontent providers expose their data as a simple table on a database model.
- xxx
- 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
- FAQ
- 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
- 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
- If you need to implement your own Cursor, you need to inherit AbstractedWindowedCursor, more details, please refer to SQLiteCursor.java. Or MatrixCursor
- 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">
否则,程序无法成功运行。
阅读(2026) | 评论(0) | 转发(0) |