码迷,mamicode.com
首页 > 移动开发 > 详细

Android查看联系人简单记录

时间:2018-12-12 21:06:35      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:ide   ons   create   信息   state   make   com   android   play   

简单实现打印联系人信息,可以作为插入联系人的基础和主要代码块,作为个人记录的小逻辑

 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     //动态获取权限
18     static final String[] PERMISSION = new String[]{
19             Manifest.permission.READ_CONTACTS,
20     };
21 
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.activity_main);
26         MyPermission();
27     }
28 
29     public void click(View view) {
30         Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
31         Cursor query = getContentResolver().query(uri, null, null, null, null);
32         if (query != null) {
33             while (query.moveToNext()) {
34                 String displayName = query.getString(query.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
35                 String displayNumber = query.getString(query.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
36                 System.out.println(displayName + ":" + displayNumber);
37             }
38         }
39 
40     }
41 
42     //动态获取权限
43     public void MyPermission() {
44         if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
45             //Android 6.0申请权限
46             ActivityCompat.requestPermissions(this, PERMISSION, 1);
47         } else {
48             Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
49         }
50     }
51 }

 

Android查看联系人简单记录

标签:ide   ons   create   信息   state   make   com   android   play   

原文地址:https://www.cnblogs.com/lgqrlchinese/p/10110930.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!