Android 调用本地通讯录,广播接收

来源:互联网 发布:陈奕迅48首选淘宝 编辑:程序博客网 时间:2024/05/16 17:55

在AndroidManifest.xml里面注册好相应的通讯录相关权限

<!-- 读取联系人权限 --><uses-permissionandroid:name="android.permission.READ_CONTACTS"/><!-- 拨打电话权限 --><uses-permissionandroid:name="android.permission.CALL_PHONE"/>

这个稍微百度下,在按钮点击或者跳转时调用下面的几句话就能弹出通讯录来,出来的样式与自己的手机有关,有搜索功能(这个手机自带)

protected static final int REQUEST_CONTACT = 1;
Intent intent = new Intent();intent.setAction(Intent.ACTION_PICK);intent.setData(ContactsContract.Contacts.CONTENT_URI);startActivityForResult(intent, REQUEST_CONTACT);

由于我是在activity下面的fragment中写的,在接收你点击某个人选择他的号码时,在主Activity中用onActivityResult,

// 接收通讯录的选择号码事件    public void onActivityResult(int requestCode, int resultCode, Intent data) {        requestCode = 1;        if (requestCode == REQUEST_CONTACT) {            if (resultCode == RESULT_OK) {                if (data == null) {                    return;                }                Uri result = data.getData();                contactId = result.getLastPathSegment();                contactName = getPhoneTel(contactId);                itemsphone = new String[thePhoneNumber.size()];                for (int i = 0; i < thePhoneNumber.size(); i++) {                    itemsphone[i] = (String) thePhoneNumber.get(i);                }                new AlertDialog.Builder(this)                        .setTitle("请选择一个有效的手机号")                        .setItems(itemsphone,                                new DialogInterface.OnClickListener() {                                    @Override                                    public void onClick(DialogInterface dialog,                                            int which) {                                        // 发送广播 传递一个号码 和 姓名                                        Intent intent = new Intent();                                        intent.setAction("com.example.BROADCAST");                                        intent.putExtra("username", contactName);                                        intent.putExtra("telephone",                                                itemsphone[which]);                                        sendBroadcast(intent);                                    }                                })                        .setNegativeButton("取消",                                new DialogInterface.OnClickListener() {                                    @Override                                    public void onClick(DialogInterface dialog,                                            int which) {                                        // 发送广播 传递一个空号码 和 空 姓名                                        Intent intent = new Intent();                                        intent.setAction("com.example.BROADCAST");                                        intent.putExtra("username", "");                                        intent.putExtra("telephone", "");                                        sendBroadcast(intent);                                    }                                }).show();            }        }    }

我在上面用到了广播

// 发送广播 传递一个空号码 和 空 姓名Intent intent = new Intent();intent.setAction("com.example.BROADCAST");intent.putExtra("username", contactName);intent.putExtra("telephone",itemsphone[which]);sendBroadcast(intent);

广播的注册

 <receiver            android:name="com.want.fragment.FragmentAffection$MyReceiver"            android:permission="com.ding.ok" >            <intent-filter>                <action android:name="com.example.BROADCAST" />            </intent-filter>        </receiver>
<uses-permission android:name="com.ding.ok" />

数据接收,在对应类com.want.fragment.FragmentAffection 写个继承BroadcastReceiver 的类接收就行了

//  接收 选择之后用户传过来的电话号码和姓名    public static class MyReceiver extends BroadcastReceiver {          @Override          public void onReceive(Context context, Intent intent) {              user = intent.getStringExtra("username");            tel = intent.getStringExtra("telephone");            txtPhoneNum.setText(tel);        }      }

这个主要是由于项目的需要,一定要在fragment中获取通讯录中的某个人的联系方式,所以使用了广播,不然上面intent调用后,回不到你原来的fragment 数据接收不到。

0 0
原创粉丝点击