Android 内容提供者之读取联系人案例

ContentProvider是不同应用程序之间进行数据交换的标准API,以Uri形式对外提供数据,允许其他应用操作本应用数据。其他应用则使用ContentResolver,并根据ContentProvider提供的Uri操作指定数据

布局文件


    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.edu.bzu.casemessagesend.MainActivity">

            android:id="@+id/LLPhoneAdd"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">
                    android:id="@+id/etPhoneNumber"
            android:inputType="phone"
            android:background="@android:drawable/edit_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:layout_weight="1"
            android:hint="请输入联系人的电话号码"
            >
           
           
                    android:id="@+id/btnAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="selectContact"
            android:src="@drawable/plus"
            android:background="@null"

            />
   

            android:id="@+id/etMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="8"
        android:inputType="textMultiLine"
        android:ems="10"
        android:layout_below="@id/LLPhoneAdd"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="19dp"
        android:hint="请输入短信内容"
         />

            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/send"
        android:background="@null"
        android:layout_below="@+id/etMessage"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/imageButton" />


联系人列表


    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_selectcontact"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.edu.bzu.casemessagesend.SelectContactActivity">

            android:id="@+id/tvChoosePeople"
        android:text="请选择联系人"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="24sp"
        android:textStyle="normal|bold" />

            android:id="@+id/lvPeople"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tvChoosePeople"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="18dp" />

主函数
package cn.edu.bzu.casemessagesend;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    public void selectContact(View view){
        Intent intent=new Intent(this,SelectContactActivity.class);
        startActivity(intent);
    }
}

实现短信发送

package cn.edu.bzu.casemessagesend;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

import cn.edu.bzu.casemessagesend.uitl.ContactUtil;

public class SelectContactActivity extends AppCompatActivity {
    private ListView lvPeople;
    private List contacts;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selectcontact);
        lvPeople=(ListView)findViewById(R.id.lvPeople);
        showContacts();
    }

   
    private void showContacts() {
        // Check the SDK version and whether the permission is already granted or not.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED ) {
            requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 1);
            //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
        } else {
            // Android version is lesser than 6.0 or the permission is already granted.
            contacts =ContactUtil.getContacts(this);
            ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,contacts);
            lvPeople.setAdapter(adapter);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,  int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            showContacts();
        } else {
            Toast.makeText(this, "Until you grant the permission, we canot display the names", Toast.LENGTH_SHORT).show();
        }

    }
}Android 内容提供者之读取联系人案例_第1张图片Android 内容提供者之读取联系人案例_第2张图片

你可能感兴趣的:(Android 内容提供者之读取联系人案例)