欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ch08联系人篇

程序员文章站 2022-09-21 09:17:33
MainActivity.javapackage com.example.ch08contactsdemo;import androidx.appcompat.app.AppCompatActivity;import android.content.ContentResolver;import android.database.Cursor;import android.os.Bundle;import android.provider.ContactsContract;import an...

ch08ContractsDemo

MainActivity.java

package com.example.ch08contactsdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView tvContact;
    private ContentResolver resolver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvContact = findViewById(R.id.tv_contact);

        //获取ContentResolver对象
        resolver = getContentResolver();
        //定义保存联系人信息的字符串变量
        StringBuffer buffer = new StringBuffer();
        //1.获取联系人的ID和姓名
        Cursor cursor = resolver.query(
                ContactsContract.Contacts.CONTENT_URI,
                null, null,
                null,null);
        //获取联系人的id和name
        while(cursor.moveToNext()){
            String id = cursor.getString(
                    cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cursor.getString(
                    cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)
            );
            if(!"".equals(name)){
                buffer.append(name + ": \n");
            }else{
                buffer.append("无名: \n");
            }
            //2.通过ID查询其对应的phone
            Cursor phoneCursor = resolver.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
                    new String[]{id},null);
            //遍历phoneCuror得到该联系人所有的电话信息
            while(phoneCursor.moveToNext()){
                //获取电话号码
                String phone = phoneCursor.getString(
                        phoneCursor.getColumnIndex(
                                ContactsContract.CommonDataKinds.Phone.NUMBER));
                buffer.append(phone + ", ");
            }
            buffer.append("\n");
            //3.通过ID查询其对应的email
            Cursor emailCursor = resolver.query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" +id,
                    null, null
            );
            while(emailCursor.moveToNext()){
                //获取该联系人的email信息
                String email = emailCursor.getString(
                        emailCursor.getColumnIndex(
                                ContactsContract.CommonDataKinds.Email.ADDRESS
                        )
                );
                buffer.append(email + ",");
            }
            //换行显示下一个联系人
            buffer.append("\n");
        }
        //将获取到的联系人信息显示在文本框中
        tvContact.setText(buffer.toString());
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ch08contactsdemo">

    <!--    添加读写联系*限  -->
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

ch08_01contactTest

ExampleInstrumentedTest.java

  • List item
package com.example.ch08_01contacttest;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        assertEquals("com.example.ch08_01contacttest", appContext.getPackageName());
    }
}

本文地址:https://blog.csdn.net/u013140841/article/details/108988384

相关标签: Android java