android通讯录项目kotlin,Android Kotlin读取联系人信息

MainActivity.kt代码

package com.example.fredric.readcontacts

import android.Manifest

import android.content.pm.PackageManager

import android.database.Cursor

import android.support.v7.app.AppCompatActivity

import android.os.Bundle

import android.provider.ContactsContract

import android.support.v4.app.ActivityCompat

import android.support.v4.content.ContextCompat

import android.widget.ArrayAdapter

import android.widget.ListView

import android.widget.Toast

class MainActivity : AppCompatActivity() {

var adapter: ArrayAdapter? = null

var contactsList = ArrayList()

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

var contactsView = findViewById(R.id.contacts_view)

adapter = ArrayAdapter(this,android.R.layout.simple_list_item_1,contactsList)

contactsView.adapter = adapter

if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_CONTACTS),1)

} else {

readContacts()

}

}

fun readContacts() {

var cursor: Cursor? = null

try{

cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null)

if(cursor != null) {

while(cursor.moveToNext()) {

var displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))

var number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))

contactsList.add(displayName+"\n"+number)

}

adapter?.notifyDataSetChanged()

}

}catch (e: Exception) {

e.printStackTrace()

}finally {

if(cursor != null) {

cursor.close()

}

}

}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) {

when(requestCode) {

1->{

if(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

readContacts()

}else {

Toast.makeText(this,"You denied the permission!!",Toast.LENGTH_SHORT).show()

}

}

else->{

}

}

}

}

activity_main.xml-->代码

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

android:id="@+id/contacts_view"

android:layout_width="match_parent"

android:layout_height="match_parent" />

AndroidManifest.xml文件代码:

package="com.example.fredric.readcontacts">

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme">

成功效果:

6aec1e357d18?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

Screen Shot 2018-07-19 at 6.22.31 AM.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值