Android studio 전화걸기 팝업추가

By | 2021년 10월 17일
Table of Contents

Android studio 전화걸기 팝업추가

전화걸기 팝업을 생성해 봅니다.

// Context 는 아무 View 예서 가져올 수 있다.
Context context = phone.getContext();

final List<String> list = new ArrayList<>();
list.add(context.getResources().getString(R.string.call_phone));
list.add(context.getResources().getString(R.string.save_phone));
final CharSequence[] items =  list.toArray(new String[0]);

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(true);
builder.setNegativeButton(R.string.cancel,
        (dialog, id) -> dialog.cancel());
builder.setTitle(phone.getText().toString());
builder.setItems(items, (dialog, pos) -> {
    String phoneNumber = phone.getText().toString();
    Intent intent;

    switch (pos) {
        case SearchRecyclerViewAdapter.CALL_PHONE:
            // ACTION_DIAL 은 실제로 전화를 거는것이 아니라 추가권한이 필요없다.
            intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
            break;
        case SearchRecyclerViewAdapter.SAVE_PHONE:
            intent = new Intent(ContactsContract.Intents.Insert.ACTION);
            intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
            intent.putExtra(ContactsContract.Intents.Insert.PHONE, phoneNumber);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
            break;
        default:
            Log.e("getKeyHash", "Error : Unexpected error");
    }
});
builder.show();

답글 남기기