在其他应用中使用自定义的Content Provider
在编写最简单的Content Provider中,是应用内部调用自定义的Content Provider,因此可以做到类似的写法:
Cursor cursor = managedQuery(MyContentProvider.CONTENT_URI, null, null,null, null);
其实,如果编写调用android系统自带的比如通讯录的Content provider,也可以有类似的写法。因为你的应用可以引用到这些类。
如果是自己的Content provider要让其他应用使用,那不可能让人家把自己的包导入进来的。怎么办呢?可以直接把这些常量的值写出来即可。这也要求,如果你想共享自己的Content provider,需要告诉人家这些常量:
- MyContentProvider.CONTENT_URI
- MyContentProvider.NAME
- MyContentProvider.START_YEAR
- MyContentProvider.DYNASTY
这是在另外一个应用中使用,代码如下:
package com.easymorse.usecp;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;public class UseCpActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);TextView textView = new TextView(this);
textView.setText(getContentProviderValues());
this.setContentView(textView);
}private CharSequence getContentProviderValues() {
StringBuilder builder = new StringBuilder();Cursor cursor = managedQuery(Uri
.parse("content://com.easymorse.cp.mycp"), null, null, null,
null);while (cursor.moveToNext()) {
builder.append(cursor.getString(cursor.getColumnIndex("name")))
.append(" | ").append(
cursor.getString(cursor
.getColumnIndex("start_year"))).append(
" | ").append(
cursor.getString(cursor.getColumnIndex("dynasty")))
.append("\n");
}return builder.toString();
}
}
这篇文章上的评论的 RSS feed TrackBack URI