Android显示通讯录,并拨通电话。

//MainActivity

package com.example.contactstest_3;

import android.Manifest;

import android.content.Intent;

import android.content.pm.PackageManager;

import android.database.Cursor;

import android.net.Uri;

import android.provider.ContactsContract;

import android.support.annotation.NonNull;

import android.support.v4.app.ActivityCompat;

import android.support.v4.content.ContextCompat;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;

import java.util.ArrayList;

import java.util.List;

public class MainActivityextends AppCompatActivity {

ArrayAdapteradapter;

    ListcontactList =new ArrayList<>();

    Stringnumber;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ListView listView = (ListView) findViewById(R.id.contact_list);

        adapter =new ArrayAdapter(this,android.R.layout.simple_list_item_1,contactList);

        listView.setAdapter(adapter);

        //点击事件

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

            public void onItemClick(AdapterView parent, View view, int position, long id) {

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

ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CALL_PHONE},2);

                }else {

call();

                }

}

});

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

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},1);

        }else {

readnew();

        }

}

//打电话

    private void call(){

Intent intent =new Intent(Intent.ACTION_CALL);

        intent.setData(Uri.parse("tel:"+number));

        startActivity(intent);

    }

private void readnew() {

Cursor cursor =null;

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

        if(cursor!=null){

while (cursor.moveToNext()){

String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

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

                Log.d("MainActivity.this",number);

                contactList.add(name+"\n"+number);

            }

adapter.notifyDataSetChanged();

        }

}

@Override

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch (requestCode){

case 1:

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

readnew();

                }else {

Toast.makeText(this,"授权失败",Toast.LENGTH_LONG).show();

                }

break;

            case 2:

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

call();

                }else {

Toast.makeText(this,"不能拨通",Toast.LENGTH_LONG).show();

                }

break;

                default:

}

}

}


//Activity.xml

    xmlns:app="http://schemas.android.com/apk/res-auto"

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

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">

        android:id="@+id/contact_list"

        android:layout_width="match_parent"

        android:layout_height="match_parent">

你可能感兴趣的:(Android显示通讯录,并拨通电话。)