Android——ContentResolver的使用——实验6

    掌握常用布局和基本控件的使用方法
    掌握文本控件和RecyclerView控件的属性功能及使用方法
    掌握ContactInfo类的使用方法
通过线性布局和相对布局来搭建通讯录界面,界面效果如下图所示。创建布局文件contact_item.xml、导入界面图片、放置界面控件、创建条目界面的背景文件。创建ContactInfo类、在该类中创建联系人信息的属性。

Android——ContentResolver的使用——实验6_第1张图片

申请读取手机通讯录的权限,将数据显示到通讯录界面上。在AndroidMainfest.xml文件中添加读取系统通讯录的权限。运行Contacts程序,查看通讯录界面效果。

Android——ContentResolver的使用——实验6_第2张图片

activity_main.xml




    
    

info.xml 




    
        
        

    

MainActivity.java 

package com.example.shiyan6;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getPermission();
        list=findViewById(R.id.list);
        list.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,true));
        MyAdapter myAdapter=new MyAdapter(this,getContacts());
        list.setAdapter(myAdapter);

    }

    public void getPermission(){
        if (ActivityCompat.checkSelfPermission(this,"android.permission.READ_CONTACTS")!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]{"android.permission.READ_CONTACTS"},666);
            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,"android.permission.READ_CONTACTS")){
                Toast.makeText(this, "需要开启联系人权限~~", Toast.LENGTH_SHORT).show();
            }else {
                ActivityCompat.requestPermissions(this,new String[]{"android.permission.READ_CONTACTS"},666);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode==666){
            for (int i=0;i getContacts(){
        List contactInfos=new ArrayList<>();
        Cursor cursor1=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
        while (cursor1.moveToNext()){
            ContactInfo contactInfo=new ContactInfo();
            String Name,phone,id;
            Name=cursor1.getString(cursor1.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
            contactInfo.setName(Name);
            id=cursor1.getString(cursor1.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
            Cursor cursor2=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+id,null,null);
            while (cursor2.moveToNext()){
                phone=cursor2.getString(cursor2.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                contactInfo.setTelephoneNumber(phone);
            }
            contactInfos.add(contactInfo);
            cursor2.close();

        }
        cursor1.close();
        return contactInfos;
    }
}

MyAdapter.java

package com.example.shiyan6;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter {
    private Context myContext;
    private List myContactInfos;
    public MyAdapter(Context context,List contactInfoList){
        this.myContext=context;
        this.myContactInfos=contactInfoList;
    }
    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=View.inflate(myContext,R.layout.info,null);
        MyHolder myHolder=new MyHolder(view);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        ContactInfo contactInfo=myContactInfos.get(position);
        holder.name.setText(contactInfo.getName());
        holder.telephoneNumber.setText(contactInfo.getTelephoneNumber());
    }

    @Override
    public int getItemCount() {
        return myContactInfos.size();
    }

    class MyHolder extends RecyclerView.ViewHolder{

        TextView name,telephoneNumber;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            name=itemView.findViewById(R.id.name);
            telephoneNumber=itemView.findViewById(R.id.telephoneNumber);
        }
    }
}

ContactInfo.java 

package com.example.shiyan6;

public class ContactInfo {
    private String name,telephoneNumber;

    public String getName() {
        return name;
    }
    public String getTelephoneNumber(){
        return telephoneNumber;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setTelephoneNumber(String telephoneNumber) {
        this.telephoneNumber = telephoneNumber;
    }
}

在配置文件里面要加上

这个是在application外面的,用途是动态权限申请的时候需要手动注册一下。

安卓SDK23以后就必须得动态申请权限,也就是Android6。这版本以前的直接静态注册,安装的时候必须同意这个权限申请,不同意就无法安装。

你可能感兴趣的:(Android,studio,android,studio,android,java)