您的位置:首页 > 移动开发 > Android开发

内容提供器 Content Provider (一)

2016-07-20 23:08 459 查看

内容提供器 Content Provider (一)案例获取手机联系人

版本:Android studio 1.5.1

获取手机联系人案例

ContentResolver

权限申明

获取手机联系人案例

做一个Listview 用来显示手机里的联系人。

这个还是很简单的

<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.example.scott.contactstest.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/contacts_view">
</ListView>
</LinearLayout>


然后再MainActivity中读取联系人信息

public class MainActivity extends AppCompatActivity {

ListView contactsView;

ArrayAdapter<String> adapter;

List<String> contactList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactsView = (ListView) findViewById(R.id.contacts_view);
adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,contactList);
contactsView.setAdapter(adapter);
readContacts();

}

private void readContacts(){
Cursor cursor = null;
try{
//通过Context中的getContentResolver()获取ContentResolver类的实例,查询法输入联系人的连接URI CONTENT_URI。
cursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);

//用cursor 将值取出 跟数据库中的用法一样
while (cursor.moveToNext()){
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactList.add(displayName+"\n"+number);
}

}catch (Exception e){
e.printStackTrace();
}finally {
if (cursor != null){
cursor.close();
}
}
}
}


后面有时间的话,对Adapter做一个总结。

最重要的别忘了在AndroidManifest.xml 文件中申明读取系统联系人权限。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.scott.contactstest">
//申明权限其它的Android 系统权限可以下载下面的文件
<uses-permission android:name="android.permission.READ_CONTACTS"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


找了到这里读取系统联系人并用列表显示出来已经完成了。当然前提你的系统联系人中要有联系人。

效果如图:



今天还遇到了一问题,就是Android Studio 自带的模拟器实在是受不了了,,之前一直是用真机调的,自认为电脑已经很给力了,,,看来还是要弄个Genymotion模拟器,明天整理一下吧。

ContentResolver

ContentResolver中提供了一系列的方法用于对数据的CRUD操作,分别是insert()、updata()、delete()、query() 。。。这个是不是跟之前的SQLiteDatabase中的方法很相似,都是使用的这几周方法对数据进行CRUD操作,只不过它们在方法参数上稍微有一些区别。

在ContentResolver中不接收表名参数,而是使用Uri 来代替。

Uri 主要由权限(authority)和路径(path)两部分组成。

权限对应程序的包名,如程序包名:com.example.soctt.content那么权限就可以命名为

com.example.scott.content.provider。

路径 如果某个程序的数据库中有两张表book,category ,路径通常是添加到权限后面的用/book、/category表示。

SO

内容Uri 的标准格式书写如下:

content://com.example.scott.content/book

content://com.example.scott.content/category

权限申明

这里是android 所有系统权限的申明。

http://pan.baidu.com/s/1nv1va6P

先大概写了一下。之后又什么补充的再添吧

关注微信公众号,每天都有优质技术文章,搞笑GIF图片推送哦。



2016-7-20

Scott

还有八天就可以全看 国际智能娱乐硬件展了。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android