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

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

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

  首先还是上一下截图

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

<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();
                    }
                }
            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值