Android基础:内容提供者简介

在Android应用中,我们可以使用显式意图(Explicit Intent)来直接访问其他应用的Activity,但是这仅限于Activity的范畴;如果需要使用其他应用的数据,还需要用到另外一种组件,这就是所谓的内容提供者(Content Provider)。

1、内容提供者(Content Porviders)简介

内容提供者主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访数据的安全性。目前,使用内容提供者是Android实现跨程序共享数据的标准方式。

内容提供者的用法一般有两种,一种是使用现有的内容提供者来读取和操作相应程序中的数据,另一种是创建自己的内容提供者给我们程序的数据提供外部访问接口。

官网定义:Content providers负责管理结构化数据的访问,Contentproviders封装数据并且提供一套定义数据安全的机制。Contentproviders是一套在不同进程间进行数据访问的接口。Contentproviders为数据跨进程访问提供了一套安全的访问机制,对数据组织和安全访问提供了可靠的保证。

每个Content Provider类都使用URI(Universal Resource Identifier,通用资源标识符)作为独立的标识,格式如:content://com.example.app.provider/table1。其他应用程序通过不同的uri访问不同的内容提供者,并获取/操作里面的数据。

2、ContentProviders的作用?

Android通过ContentProvider来管理数据诸如音频、视频、图片和通讯录等。还可以通过ContentProvider来访问SQLite数据库等。

在以下情况下你需要使用ContentProviders:

  • 你想为其他应用程序提供复杂数据或文件
  • 你想允许用户从你的应用程序中拷贝复杂数据到其他的应用中
  • 你想使用搜索框架提供自定义的查询建议功能

3、内容提供者方法介绍

(1)onCreate()

初始化内容提供者的时候调用。通常会在这里完成对数据库的创建和升级等操作,返回true表示内容提供者初始化成功,返回false则表示失败。注意,只有当存在ContentResolver尝试访问我们程序中的数据时,内容提供者才会被初始化。

(2)query()

从内容提供者中查询数据。使用uri参数来确定查询哪张表,projection参数用于确定查询哪些列,selection和selectionArgs参数用于约束查询哪些行,sortOrder参数用于对结果进行排序,查询的结果存放在Cursor对象中返回。

(3)insert()

向内容提供者中添加一条数据。使用uri参数来确定要添加的表,待添加的数据保存在values参数中。添加完成后,返回一个用于表示这条新记录的URI。

(4)update()

更新内容提供者中的数据。使用uri参数确定更新哪一张表中的数据,新数据保存在values参数中,selection和selectionArgs参数用于约束更新哪些行,受影响的行数将作为返回值返回。

(5)delete()

从内容提供者中删除数据。使用uri参数来确定删除哪一张表中的数据,selection和selectionArgs参数用于约束删除哪些行,被删除的行数作为返回值返回。

(6)getType()

根据传入的内容URI来返回相应的MIME类型。


4、读取系统联系人

新建一个AndroidApplication Project来显示联系人列表。

(1)修改activity_main.xml中代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.contactstest.MainActivity" >

    <ListView 
        android:id="@+id/contacts_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">        
    </ListView>
    
</LinearLayout>

(2)修改MainActivity.java中的代码:

public classMainActivity extends Activity {
  
   ListViewcontactsView;
   ArrayAdapter<String>adapter;
   List<String>contactsList=newArrayList<String>();
 
   @Override
   protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      contactsView=(ListView)findViewById(R.id.contacts_view);
      adapter=newArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactsList);
      contactsView.setAdapter(adapter);
      readContacts();
   }
 
   private void readContacts() {
      Cursorcursor=null;
      try {
         //查询联系人数据    cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
         while(cursor.moveToNext()){
            //获取联系人姓名
            StringdisplayName=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            //获取联系人号码
            Stringnumber=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contactsList.add(displayName+"\n"+number);
         }
      }catch(Exception e) {
         e.printStackTrace();
      }finally{
         if (cursor != null) {
            cursor.close();
         }
      }
   }
 
   @Override
   public booleanonCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
 
   @Override
   public booleanonOptionsItemSelected(MenuItem item) {
      int id = item.getItemId();
      if (id == R.id.action_settings){
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
}

(3)在AndroidManifest.xml文件中添加访问系统通讯录的权限:

<uses-permissionandroid:name="android.permission.READ_CONTACTS"/>

运行程序就可以访问到系统的联系人列表。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值