内容提供器 Content Provider (一)
来源:互联网 发布:淘宝数据分析表 编辑:程序博客网 时间:2024/05/16 00:48
内容提供器 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
还有八天就可以全看 国际智能娱乐硬件展了。。。
- 内容提供器 Content Provider (一)
- 内容提供器 Content Provider 入门(一)
- Android之内容提供器Content Provider详解(一)
- 【Android】内容提供器(Content Provider)
- 内容提供器(Content Provider)
- 内容提供器(Content Provider)
- Content Provider(内容提供器)
- 内容提供器---Content Provider
- 内容提供器 Content Provider
- Content Provider 内容提供器
- Content Provider Basics(内容提供器基础)
- Creating a Content Provider(构建内容提供器)
- 内容提供器 Content Provider 入门(二)
- 内容提供器(Content Provider)--跨程序共享数据
- Android — 之内容提供器(Content Provider)
- Android之内容提供器Content Provider详解(二)
- Android组件----内容提供器Content Provider
- 自定义Content Provider(内容提供器)
- 《转》【笔记2】关于Linux 802.11n csi tool的安装注意事项
- C++ Socket 发送 http post请求
- HDOJ-----5053立方和公式
- 解决httpclient超时设置不生效的问题
- EXTJS4.0.7开发积累(6)
- 内容提供器 Content Provider (一)
- JTree的用法实践
- python mvc框架django搭建项目
- 《转》【笔记3】获取csi文件,并用MATLAB处理
- 漫谈处理器体系结构
- C语言中体现面向对象的地方
- Lua-4---面向对象
- idea 14.0破解
- 细剖最短路【闲谈,权当是饭后杂文】