少年击剑更吹箫,剑气箫心一例消。这篇文章主要讲述Android查看联系人简单记录相关的知识,希望能为你提供帮助。
简单实现打印联系人信息,可以作为插入联系人的基础和主要代码块,作为个人记录的小逻辑
1 package com.lgqrlchinese.contactstest; 2 3 import android.Manifest; 4 import android.content.pm.PackageManager; 5 import android.database.Cursor; 6 import android.net.Uri; 7 import android.provider.ContactsContract; 8 import android.support.v4.app.ActivityCompat; 9 import android.support.v4.content.ContextCompat; 10 import android.support.v7.app.AppCompatActivity; 11 import android.os.Bundle; 12 import android.view.View; 13 import android.widget.Toast; 14 15 public class MainActivity extends AppCompatActivity { 16 17//动态获取权限 18static final String[] PERMISSION = new String[]{ 19Manifest.permission.READ_CONTACTS, 20}; 21 22@Override 23protected void onCreate(Bundle savedInstanceState) { 24super.onCreate(savedInstanceState); 25setContentView(R.layout.activity_main); 26MyPermission(); 27} 28 29public void click(View view) { 30Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 31Cursor query = getContentResolver().query(uri, null, null, null, null); 32if (query != null) { 33while (query.moveToNext()) { 34String displayName = query.getString(query.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 35String displayNumber = query.getString(query.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 36System.out.println(displayName + ":" + displayNumber); 37} 38} 39 40} 41 42//动态获取权限 43public void MyPermission() { 44if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { 45//Android 6.0申请权限 46ActivityCompat.requestPermissions(this, PERMISSION, 1); 47} else { 48Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show(); 49} 50} 51 }
【Android查看联系人简单记录】
推荐阅读
- html调用安卓提示音报错E/MediaPlayerNative: error(-38, 0),E/MediaPlayerNative: error(1, -19)
- Android知识整理
- 安卓响铃和震动
- cookie mapping 原理理解
- AutoMapper C#实体映射
- iView webapp / Mint UI / MUI[前端UI]
- Android学习第九天
- Android-Intent意图传递数据
- 安卓手机通讯录联系人恢复