您的位置:首页 > 编程语言 > PHP开发

ContentProvider浅谈+实例

2015-11-02 09:06 645 查看
作为安卓四大组件之一,ContentProvider的用处也不少,ContentProvider用于保存和检索数据,是安卓系统中不同应用程序之间共享数据的接口。
最直观的应用就是当你发送短信时需要用到联系人的相关信息,此时通过ContentProvider提供的接口访问Android系统中的电话簿,并从中选中了联系人。
Android系统对一系列公共的常用数据类型提供了对应的ContentProvider接口,都定义在android.provider包下。
ContentProvider实现共享数据:ContentProvider提供了一组应用程序之间能访问的接口,应用程序通过ContentProvider把当前应用中的数据共享给其他应用程序访问,二其他应用程序也可以通过ContentProvider对指定的应用程序进行访问。将自己的数据公开给其他应用使用有2种方法,1.定义自己的ContentProvider子类,2.将当前应用的数据添加到已有的ContentProvider中。

下面是一个用ContentProvider访问手机联系人的例子。

MainActivity代码:

public class MainActivity extends Activity {

private String[] columns={Contacts._ID,
Contacts.DISPLAY_NAME,
Phone.NUMBER,
Phone.CONTACT_ID
};
private ListView listview;
private Dialog dialog;
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=(ListView)findViewById(R.id.list1);
dialog=new Dialog(MainActivity.this);
dialog.setTitle("电话");
dialog.setContentView(R.layout.content_dialog);
dialog.setCanceledOnTouchOutside(true);
showinfo();
listview.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
dialog.show();

}
});
}
private void showinfo(){
ArrayList<HashMap<String ,String>> list=new ArrayList<HashMap<String, String>>();
ContentResolver resolver=getContentResolver();
Cursor cursor=resolver.query(Contacts.CONTENT_URI, null,null,null,null);
while(cursor.moveToNext()){
HashMap<String,String> map=new HashMap<String, String>();
String liststr="";
int idindex=cursor.getColumnIndex(columns[0]);
int nameindex=cursor.getColumnIndex(columns[1]);
int id=cursor.getInt(idindex);
String name=cursor.getString(nameindex);
Cursor phone=resolver.query(Phone.CONTENT_URI, null,columns[3]+"="+id,null,null);
while(phone.moveToNext()){
int phonenumberindex=phone.getColumnIndex(columns[2]);
String phonenumber=phone.getString(phonenumberindex);
liststr+=phonenumber;
}
map.put("name", name);
map.put("phonenumber", liststr);
list.add(map);
}
cursor.close();
SimpleAdapter simpleAdapter=new SimpleAdapter(this, list, R.layout.simple_adapter_item, new String[]{"name","phonenumber"}, new int[]{R.id.row_text1,R.id.row_text2});
listview.setAdapter(simpleAdapter);
}

}

这段主要代码中,Dialog是一个点击联系人即可出现的一个悬浮对话框,即红色部分。

联系人列表主要是showinfo()方法,即蓝色部分,ListView中用的是SimpleAdapter适配。

对应的Xml文件比较简单,在这里也给出:




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"
tools:context=".MainActivity" >

<ListView
android:id="@+id/list1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawVerticalTrack="true">

</ListView>

</RelativeLayout>

content_dialog.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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="--电话号码--"/>

</LinearLayout>

simple_adapter_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" >
<TextView
android:id="@+id/row_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="--------"
/>
<TextView
android:id="@+id/row_text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="--------"
/>

</LinearLayout>

同时,需要进行权限设置,在AndroidMainfest.xml里,

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

这样就ok了,可以访问系统的电话簿了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息