Android 查看并获取联系人电话示例

本示例的布局文件如下:




    

    

    

主程序文件的源代码:

package com.toby.personal.testlistview;

import android.Manifest;
import android.app.Activity;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    final private static String TAG = "Toby_Test";
    final private static int PICK_CONTACT = 44;
    final private static int REQUEST_CODE_ASK_PERMISSIONS = 20170404;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void find(View view) {
        int hasWriteContactsPermission = checkSelfPermission(Manifest.permission.READ_CONTACTS);
        if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
                    REQUEST_CODE_ASK_PERMISSIONS);
            return;
        }

        pickContact();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case PICK_CONTACT:
                if (resultCode == Activity.RESULT_OK) {

                    Uri contactData = data.getData();
                    CursorLoader cursorLoader =
                            new CursorLoader(this, contactData, null, null, null, null);
                    Cursor cursor = cursorLoader.loadInBackground();

                    if (cursor.moveToFirst()) {
                        String contactId = cursor.getString(
                                cursor.getColumnIndex(ContactsContract.Contacts._ID));

                        String name = cursor.getString(
                                cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

                        String phoneNumber = "";
                        Cursor phones = getContentResolver().query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                        + " = " + contactId, null, null);

                        if (phones.moveToFirst()) {
                            phoneNumber = phones.getString(phones.getColumnIndex(
                                    ContactsContract.CommonDataKinds.Phone.NUMBER));
                        }
                        phones.close();

                        TextView show = (TextView) findViewById(R.id.show);
                        show.setText(name);

                        TextView phone = (TextView) findViewById(R.id.phone);
                        phone.setText(phoneNumber);
                    }

                    cursor.close();
                }
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_PERMISSIONS:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    pickContact();
                } else {
                    Toast.makeText(MainActivity.this, "READ_CONTACTS Denied", Toast.LENGTH_SHORT)
                            .show();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    private void pickContact() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.setType("vnd.android.cursor.item/phone");
        startActivityForResult(intent, PICK_CONTACT);
    }

    public void home(View view) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }
}

我们需要在 AndroidManifest.xml 加入权限声明:


程序的运行效果,各位可以自行运行测试,如有疑问可以随时给我留言。

参考文献:《疯狂Android讲义(第2版)》

你可能感兴趣的:(Android 查看并获取联系人电话示例)