ApiDemos学习知识点Content-PickContacts(5)

时间:2021-11-12 09:26:10

本Activity是以获取联系人信息为例,通过设置数据类型来选择能够打开该类型的应用,通过设置不同的数据类型打开联系人的不同信息。

注意:使用这种方式是不需要添加读取联系人的权限的,因为我们调用的是系统联系人的应用

  首先还是上一下截图

ApiDemos学习知识点Content-PickContacts(5)

下面看一下布局和代码解析

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">


<TextView
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="4dip"
android:text="@string/pick_contact_msg"/>

<Button android:id="@+id/pick_contact"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/pick_contact">
<requestFocus />
</Button>


<Button android:id="@+id/pick_person"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/pick_person">
</Button>


<Button android:id="@+id/pick_phone"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/pick_phone">
</Button>


<Button android:id="@+id/pick_address"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/pick_address">
</Button>
</LinearLayout>

public class PickContact extends Activity {
Toast mToast;
ResultDisplayer mPendingResult;
/*
     * 定义一个类,实现对组件的点击监听,用于在点击事件时打开相应的activity
     */
class ResultDisplayer implements OnClickListener {
String mMsg;
String mMimeType;

ResultDisplayer(String msg, String mimeType) {
mMsg = msg;
mMimeType = mimeType;
}
  // ACTION_GET_CONTENT:允许用户通过设置数据类型来选择能够打开这种类型的数据的
            // activity。与ACTION_PICK的区别在于:ACTION_PICK只是指明了我们所预期的数据
            // 类型,而ACTION_GET_CONTENT则允许用户从数据的Uri中选择相应的类型
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(mMimeType);
mPendingResult = this;
startActivityForResult(intent, 1);
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.pick_contact);

// Watch for button clicks.
((Button)findViewById(R.id.pick_contact)).setOnClickListener(
new ResultDisplayer("Selected contact",
ContactsContract.Contacts.CONTENT_ITEM_TYPE));
((Button)findViewById(R.id.pick_person)).setOnClickListener(
new ResultDisplayer("Selected person",
"vnd.android.cursor.item/person"));
((Button)findViewById(R.id.pick_phone)).setOnClickListener(
new ResultDisplayer("Selected phone",
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE));
((Button)findViewById(R.id.pick_address)).setOnClickListener(
new ResultDisplayer("Selected address",
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE));
}
  /*
     * 将我们所预期的数据查询出来,当我们点击某条数据时,用一个Toast显示一下该条记录的Id和uri信息
     */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver().query(uri, new String[] { BaseColumns._ID },
null, null, null);
if (c != null && c.moveToFirst()) {
int id = c.getInt(0);
if (mToast != null) {
mToast.cancel();
}
String txt = mPendingResult.mMsg + ":\n" + uri + "\nid: " + id;
mToast = Toast.makeText(this, txt, Toast.LENGTH_LONG);
mToast.show();
}
} finally {
if (c != null) {
c.close();
}
}
}
}
}
}