Android Api Demos登顶之路(六十一)Content-->PickContacts

这个demo演示了如何通过设置数据类型来选择能够打开该类型的应用
* 以联系人为例,通过设置不同的数据类型打开我们所关心的联系人的不同信息
* 注意:使用这种方式是不需要添加读取联系人的权限的,因为我们调用的是系统联系人的应用
activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/info" />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Pick a contact"
        android:id="@+id/pick_contact"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Pick a person"
        android:id="@+id/pick_person"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Pick a phone"
        android:id="@+id/pick_phone"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Pick an address"
        android:id="@+id/pick_address"/>

</LinearLayout>

MainActivity

public class MainActivity extends Activity {
    private Toast mToast;
    private ResultDisplay mPendingResult;

    /*
     * 定义一个类,实现对组件的点击监听,用于在点击事件时打开相应的activity
     */
    class ResultDisplay implements OnClickListener {
        private String msg;
        private String mimeTypes;

        public ResultDisplay(String msg, String mimeTypes) {
            super();
            this.msg = msg;
            this.mimeTypes = mimeTypes;
        }

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

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

        Button pick_contact = (Button) findViewById(R.id.pick_contact);
        Button pick_person = (Button) findViewById(R.id.pick_person);
        Button pick_phone = (Button) findViewById(R.id.pick_phone);
        Button pick_address = (Button) findViewById(R.id.pick_address);

        pick_contact.setOnClickListener(new ResultDisplay("Selected Contact",
                ContactsContract.Contacts.CONTENT_ITEM_TYPE));
        pick_person.setOnClickListener(new ResultDisplay("Selected Person",
                "vnd.android.cursor.item/person"));
        pick_phone.setOnClickListener(new ResultDisplay("Selected Phone",
                ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE));
        pick_address
                .setOnClickListener(new ResultDisplay(
                        "Selected Address",
                        ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE));
    }

    /*
     * 将我们所预期的数据查询出来,当我们点击某条数据时,用一个Toast显示一下该条记录的Id和uri信息
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, 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.msg+":\n"+uri+":\nid"+id;
                        mToast=Toast.makeText(this, txt, 0);
                        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、付费专栏及课程。

余额充值