Content Provider提供了一种多应用间数据共享的方式,比如:联系人信息可以被多个应用程序访问。Content Provider是个实现了一组用于提供其他应用程序存取数据的标准方法的类。

下面来看一个简单的实例: MainActivity.java

package com.app.contentprovidetest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast; public class MainActivity extends Activity {
//声明ContentResolver对象
private ContentResolver mContentResolver= null;
Cursor cursor ;
//声明联系人 Uri位置
private Uri uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获得ContentResolver对象
mContentResolver=getContentResolver();
uri =ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
//cursor取数据
//(uri地址,显示字段,查询条件,条件参数,排序)
cursor = mContentResolver.query(uri, null, null, null, null);
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
if(cursor.getCount()>0){
while(cursor.moveToNext()){//游标下标移动
String name =cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));
String phone =cursor.getString(cursor.getColumnIndex(Phone.DATA));
Log.i("xxxxxx", "name:"+name+","+"phone"+phone);
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", name);
map.put("phone", phone);
list.add(map);
}
ListView listView1 =(ListView) findViewById(R.id.listView1);
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list, R.layout.listview_item,
new String[]{"name","phone"}, new int[]{R.id.textView1,R.id.textView2});
listView1.setAdapter(adapter);
}
insert();//添加系统ContentProvider 内容
}
private void insert() {
//获得ContentResolver对象实例
ContentResolver cr=getContentResolver();
//将添加的信息封装到ContentValues对象
ContentValues values = new ContentValues();
//定义Uri
Uri uri = Contacts.People.CONTENT_URI;
//添加姓名
values.put(People.NAME, "Android");
//添加电话号
values.put(People.NUMBER, "12345");
//执行添加操作
cr.insert(uri, values);
Toast.makeText(getApplicationContext(), "添加成功" , Toast.LENGTH_SHORT).show();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

R.Layout.XML文件 listview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> </LinearLayout> </LinearLayout>

activity_main.xml

<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1" >
</ListView> </RelativeLayout>

ANDROID content provide 使用实例的更多相关文章

  1. Android应用程序组件Content Provider应用实例

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6950440 文简要介绍了Android应用程序 ...

  2. Android Content Provider Guides

    Android Content Provider Guides Content Providers管理对结构化数据集的访问.它们包装数据,并且提供一种定义数据安全的机制. Content provid ...

  3. Android Content Provider基础

    Android Content Provider基础 Content Providers Content providers管理对一个结构化的数据集合的访问.它们封装了数据,并且提供了保护数据安全性的 ...

  4. (转载)Android content provider基础与使用

    android有一个独特之处就是,数据库只能被它的创建者所使用,其他的应用是不能访问到的,所以如果你想实现不同应用之间的数据共享,就不得不用content provider了.在Android中,co ...

  5. android.content.Context 含义及使用

    Context字面意思上下文,位于framework package的android.content.Context中,其实该类为LONG型,类似Win32中的Handle句柄,很多方法需要通过Con ...

  6. Android传感器编程带实例

    看了程序人生 网站的 编程高手的编程感悟 深有感触,好像也是一个android 程序员写的,推荐大家也看看.话不多说,还是言归正传吧. 一.前言 我很喜欢电脑,可是笔记本还是太大,笔记本电脑再小还是要 ...

  7. Android传感器编程带实例(转)

    源:http://www.cnblogs.com/xiaochao1234/p/3894751.html 看了程序人生 网站的 编程高手的编程感悟 深有感触,好像也是一个android 程序员写的,推 ...

  8. Android EventBus 3.0 实例使用详解

    EventBus的使用和原理在网上有很多的博客了,其中泓洋大哥和启舰写的非常非常棒,我也是跟着他们的博客学会的EventBus,因为是第一次接触并使用EventBus,所以我写的更多是如何使用,源码解 ...

  9. 6、Android Content Provider测试

    如果你的应用中使用了Content Provider来与其他应用进行数据交互,你需要对Content Provider进行测试来确保正常工作. 创建Content Provider整合测试 在Andr ...

随机推荐

  1. LOJ550 Matching 构造

    传送门 题意:$T$组询问,每组询问给出一个$N \times M$的网格和一个$K$,每一次你可以消除网格中的两个块,如果两个块的曼哈顿距离小于$K$,则不会得到分数,否则得到等同于它们曼哈顿距离的 ...

  2. 算法相关——Java排序算法之桶排序(一)

    (代码中对应一个数组的下标),将每个元素放入对应桶中,再将所有元素按顺序输出(代码中则按顺序将数组i下标输出arrary[i]次),即为{0,1,3,5,5,6,9}. 1.2  代码实现 /* *@ ...

  3. Android开发——ListView使用技巧总结(一)

    )还有一点就是要控制异步任务的执行频率,因为当用户频繁的上下滑动,会瞬间产生上百个异步任务,会带来无意义的大量的UI更新操作,因此可以考虑在列表滑动时停止进行异步任务,直到列表停下来. //判断列表的 ...

  4. CRC---循环冗余校验

    typedef unsigned char uchar; typedef unsigned int uint; typedef unsigned short uInt16; uint crc; // ...

  5. 从零开始搭建属于你的React/redux/webpack脚手架

    大家好,我是苏南,今天要给大家分享的是<<我的react入门到放弃之路>>,当然,也不是真的放弃啦--哈哈,这篇博客原本是从17年初写的,一直没有在csdn发布,希望今天不会太 ...

  6. webvirtmgr-重命名kvm虚拟机的名称

    之前部署了Webvirtmgr平台管理kvm虚拟机,由于虚拟机在创建时名称是顺便起的,后续在虚拟机上部署了部分业务.为了便于管理,最好将虚拟机的名称重置下. 现在说下如何修改kvm中虚拟机的名称: 比 ...

  7. C_数据结构_递归实现累加

    # include <stdio.h> long sum(int n) { //用递归实现: ) ; else ) + n; /* 用for循环实现: long s = 0; int i; ...

  8. Python-元组-10

    元祖 Why:对于容器型数据类型list,无论谁都可以对其增删改查,那么有一些重要的数据放在list中是不安全的,所以需要一种容器类的数据类型存放重要的数据,创建之初只能查看而不能增删改,这种数据类型 ...

  9. 个人作业week7——前端开发感想总结

    个人作业week7——前端开发感想总结 1. 反思 首先要谈谈在这次团队项目的工作中,我这边出现过的较为严重的一个问题:我和HoerWing (后端担当)合作时,最初因为我没有使用github(始终连 ...

  10. Linux课题实践三——程序破解

    2.3   程序破解 20135318 刘浩晨 1.     掌握NOP.JNE.JE.JMP.CMP汇编指令的机器码 NOP:NOP指令即“空指令”.执行到NOP指令时,CPU什么也不做,仅仅当做一 ...