iOS를 사랑하는 AOS 개발자

[Android] 안드로이드 앱 내에서 전화걸기 본문

Android ( JAVA )/개발

[Android] 안드로이드 앱 내에서 전화걸기

아사안개 2021. 12. 17. 16:10
반응형
SMALL

👼🏻 초보 안드로이드 개발자가 매번 구글링하기 싫어서 정리하는 블로그 👼🏻 

 

🌟 스샷은 아이폰

💥 개발은 안드로이드

 

 

 

 

 

 

 

 

 

 

👈🏻 이렇게 뜨는거 할꺼에요

 

 

전화를 걸고 다이얼을 여는건 AndroidManifest에 permission 셋팅을 합니다.

AndroidManifest.xml
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.DIAL_PHONE" />

 

메니페스트에 퍼미션을 셋팅했다면 이제 뷰를 그립시다.

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:padding="20dp">

    <EditText
        android:id="@+id/et_num"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="번호를 입력하세요. (' - ' 생략, 숫자만)"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button
            android:id="@+id/btn_call"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="전화 바로 걸기"
            android:layout_marginRight="10dp"
            />
        <Button
            android:id="@+id/btn_dial"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="다이얼로 가기"
            android:layout_marginLeft="10dp"
            />
    </LinearLayout>

</LinearLayout>

 

뷰를 다 그렸으면 이제 액티비티로 넘어갑니다.

반응형
MainActivity
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    // 변수 선언
    EditText et_num;
    Button btn_call, btn_dial;
    String telNum = "";

    static final int PERMISSIONS_CALL_PHONE = 1;

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

        // xml 과 연결
        et_num = findViewById(R.id.et_num);
        btn_call = findViewById(R.id.btn_call);
        btn_dial = findViewById(R.id.btn_dial);

        // 클릭 이벤트 설정
        btn_call.setOnClickListener(this);
        btn_dial.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        // 입력번호 저장
        telNum = et_num.getText().toString();
        switch (view.getId()) {
            // 전화 바로 걸기
            // :: 전화 바로 걸기는 시용자에게 권한이 필요하기때문에 권한여부를 먼저 확인한다.
            case R.id.btn_call:
                if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, PERMISSIONS_CALL_PHONE);
                } else {
                    Intent callIntent = new Intent(Intent.ACTION_CALL);
                    callIntent.setData(Uri.parse("tel:" + telNum));
                    startActivity(callIntent);
                }
                break;
            // 다이얼로 가기
            case R.id.btn_dial:
                Intent dialIntent = new Intent(Intent.ACTION_DIAL);
                dialIntent.setData(Uri.parse("tel:" + telNum));
                startActivity(dialIntent);
                break;
        }
    }
}
SMALL

코드는 끝입니다.

실행 결과는 직접해보세요!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 

👼🏻 초보 안드로이드 개발자가 매번 구글링하기 싫어서 정리하는 블로그 👼🏻 

반응형
LIST