1. 程式人生 > >Android App實現點選撥號、複製剪貼簿、新增手機聯絡人

Android App實現點選撥號、複製剪貼簿、新增手機聯絡人

1.概述

今天介紹一下實現Android App內實現點選撥號、複製剪貼簿、新增手機聯絡人等相關操作

2.實現效果

沒有gif圖




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