(Android) ContentProvider 实例

本文介绍了一个具体的Android ContentProvider实现案例,演示了如何通过ContentProvider在两个应用间进行数据的插入、更新、删除和查询操作。示例中详细展示了ContentProvider的创建过程,包括UriMatcher的使用、不同类型数据的处理方式等。

ContentProvider 用于应用程序(Android Application)之间传递数据,包括Insert, update, delete, query。

下面的例子是在两个应用之间传递数据。

 

应用一(创建ContentProviderTestA)

TestContentProvider.java

public class TestContentProvider extends ContentProvider {

 private final static UriMatcher URI_MATCHER;
 private final static int ALL_MESSAGES = 1;
 private final static int SPECIFIC_MESSAGE = 2;

 static {
  URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
  URI_MATCHER.addURI("com.example.contentprovidertesta", "item", ALL_MESSAGES);
  URI_MATCHER.addURI("com.example.contentprovidertesta", "specialitem", SPECIFIC_MESSAGE);
 }

 public static final Uri CONTENT_URI = Uri
   .parse("content://com.example.contentprovidertesta");

 private final static String DEBUG = "ContentProvider";

 @Override
 public String getType(Uri uri) {
  switch (URI_MATCHER.match(uri)) {
  case SPECIFIC_MESSAGE:
   Log.d(DEBUG, "specialitem");
   return "specialitem";
  case ALL_MESSAGES:
   Log.d(DEBUG, "item");
   return "item/normal";
  }
  return "item";
 }

 @Override
 public int delete(Uri uri, String selection, String[] selectionArgs) {
  Log.d(DEBUG, uri.toString());
  if (getType(uri).equals("specialitem")) {
   Log.d(DEBUG, "-----------delete sepcial items-------------");
  } else {
   Log.d(DEBUG, "-----------delete-------------");
  }
  return 0;
 }

 @Override
 public Uri insert(Uri uri, ContentValues values) {
  return null;
 }

 @Override
 public boolean onCreate() {
  return false;
 }

 @Override
 public Cursor query(Uri uri, String[] projection, String selection,
   String[] selectionArgs, String sortOrder) {
  return null;
 }

 @Override
 public int update(Uri uri, ContentValues values, String selection,
   String[] selectionArgs) {
  return 0;
 }
}

AndroidManifest.xml

<application>

<provider
            android:name="com.example.contentprovidertesta.TestContentProvider"
            android:authorities="com.example.contentprovidertesta" />

</application>

 

应用二 ContentProviderTest

public class MainActivity extends Activity {

 private final Uri testallmessage = Uri
   .parse("content://com.example.contentprovidertesta/item");
 private final Uri testspecialmessage = Uri
   .parse("content://com.example.contentprovidertesta/specialitem");

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }

 @Override
 protected void onResume() {
  super.onResume();
  testContentProvider();
 }

 private void testContentProvider() {
  getContentResolver().delete(testallmessage, null, null);
  getContentResolver().delete(testspecialmessage, null, null);
 }

}

 

当安装两个应用后,再运行应用二出现的log,

content://com.example.contentprovidertesta/item
item
-----------delete-------------
content://com.example.contentprovidertesta/specialitem
specialitem
-----------delete sepcial items-------------

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值