一天一点Android知识(内容提供器)
来源:互联网 发布:手机淘宝 没有链接 编辑:程序博客网 时间:2024/04/30 05:50
内容提供器的用法有两种,一种是使用现有的内容提供器来读取和操作相应程序中的数据,另外一种是创建自己的内容提供器给我们程序的数据提供外部访问接口。
MainActivity.java
package com.example.contactstest;import android.Manifest;import android.content.pm.PackageManager;import android.database.Cursor;import android.provider.ContactsContract;import android.support.annotation.NonNull;import android.support.v4.app.ActivityCompat;import android.support.v4.content.ContextCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity { ArrayAdapter<String> adapter; List<String> contactList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView contactView = (ListView)findViewById(R.id.contacts_view); adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contactList); contactView.setAdapter(adapter); if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},1); }else{ readContacts(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode){ case 1: if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){ readContacts(); }else{ Toast.makeText(this,"You denied the permission",Toast.LENGTH_SHORT).show(); }break; default: } } private void readContacts(){ Cursor cursor = null; try{ cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null); if (cursor!=null){ 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); } adapter.notifyDataSetChanged(); } } catch (Exception e){ e.printStackTrace(); }finally { if (cursor != null){ cursor.close(); } } }}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/contacts_view" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView></LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.contactstest"> <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>
错误日志
展示系统联系人信息
0 0
- 一天一点Android知识(内容提供器)
- 一天一点Android知识
- 一天一点android知识(Broadcast)
- 一天一点Android知识(SharedPreferences存储)
- 一天一点Android知识之intent(1)
- 一天一点Android知识(实现强制下线功能)
- 一天一点Android知识(SQLite数据库存储)
- 一天一点Android知识(SQLite数据库存储2)
- Android 内容提供器---内容提供器基础(概述)
- Android 内容提供器---创建内容提供器(概要)
- Android 内容提供器---内容提供器基础(概述) .
- Android 内容提供器---创建内容提供器(概要)
- Android 内容提供器---内容提供器基础(内容提供器权限)
- Android 内容提供器---创建内容提供器(实现内容提供器权限)
- Android 内容提供器---内容提供器基础(内容提供器权限)
- Android 内容提供器---创建内容提供器(实现内容提供器权限)
- android内容提供器
- Android内容提供器
- [Node.js]模块
- 补码的计算原理(另附:原码、反码、补码如何产生的)
- vs2015查找功能失效网络解决方案整理
- Unicode 和 UTF-8
- 《MySQL必知必会》学习笔记三------掌握部分
- 一天一点Android知识(内容提供器)
- AI - TensorFlow
- 组合数学 POJ 1942 Paths on a Grid
- epoll多进程及惊群问题
- 2.2 元组
- Runloop和AutoreleasePool
- 《HelloGitHub月刊》第10期
- 教女朋友学Python(10)——我都最后一期了,你都不知道对象是什么
- 第17章 控件模版(7)——通过样式自动应用模版