Android App实现点击拨号、复制剪贴板、添加手机联系人

1.概述

今天介绍一下实现Android App内实现点击拨号、复制剪贴板、添加手机联系人等相关操作


2.实现效果

没有gif图

Android App实现点击拨号、复制剪贴板、添加手机联系人_第1张图片

Android App实现点击拨号、复制剪贴板、添加手机联系人_第2张图片

Android App实现点击拨号、复制剪贴板、添加手机联系人_第3张图片

3.实现步骤


直接上代码:


private void initService() {
    String[] info = new String[]{"呼叫", "复制", "添加到通讯录"};
    final String[] addInfo = new String[]{"新建联系人", "添加到已有联系人"};
    final String phone = mServicePhone.getText().toString().trim();//获取手机号码兑对象
    //创建粘贴板对象

    //点击弹出对话框
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setItems(info, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int position) {
            switch (position) {
                case 0: //呼叫
                    final Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                    call.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(call);
                    break;
                case 1://复制
                    ClipboardManager copy = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                    copy.setText(phone);
                    String text = copy.getText().toString().trim();
                    MyToast.show(getContext(), "已复制" + text);
                    break;
                case 2://添加通讯录
                    final AlertDialog.Builder add = new AlertDialog.Builder(getContext());
                    add.setItems(addInfo, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            switch (i) {
                                case 0://新建联系人
                                    Intent addNew = new Intent(Intent.ACTION_INSERT,
                                            Uri.withAppendedPath(Uri.parse("content://com.android.contacts"), "contacts"));
                                    addNew.setType("vnd.android.cursor.dir/person");
                                    addNew.putExtra(android.provider.ContactsContract.Intents.Insert.SECONDARY_PHONE, phone);
                                    startActivity(addNew);
                                    break;
                                case 1://添加已有联系人
                                    Intent oldConstantIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
                                    oldConstantIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
                                    oldConstantIntent.putExtra(ContactsContract.Intents.Insert.PHONE, phone);
                                    oldConstantIntent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, 3);
                                    startActivity(oldConstantIntent);
                                    break;
                            }
                        }
                    });
                    AlertDialog alertDialog = add.create();
                    alertDialog.show();
                    break;
            }
        }
    });
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}


你可能感兴趣的:(android)