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     //动态获取权限
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 }

 

posted @ 2018-12-12 20:49  龙谷情Sinoam  阅读(248)  评论(0编辑  收藏  举报
Smiley face