728x90
반응형
◾ 보통 권한 / 위험 권한
📃전화 걸기 화면 실행
📝AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.project.test02">
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Day38">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
📝activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:layout_margin="10dp"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="전화 기능 연결 테스트"
android:textSize="25dp" />
<EditText
android:id="@+id/et_dial"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="전화번호를 입력하세요"
android:textSize="20dp" />
<Button
android:id="@+id/btn_call"
android:onClick="onCallClick"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="전화 화면 열기"
android:textSize="20dp" />
</LinearLayout>
📝MainActivity.java
package com.project.test02;
import androidx.annotation.NonNull;
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;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
static final int REQCODE_PERMISSION_CALLPHONE = 1;
EditText et_Number;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn_call);
et_Number = findViewById(R.id.et_dial);
}
public void onCallClick(View view)
{
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) !=
PackageManager.PERMISSION_GRANTED)
{
//CALL PHONE권한이 없는 경우 권한 승인을 거절한 이력을 확인한다.
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.CALL_PHONE))
{
Toast.makeText(MainActivity.this, "권한이 없습니다.", Toast.LENGTH_SHORT).show();
}
else
{
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},
REQCODE_PERMISSION_CALLPHONE);
}
}
else
{
//권한을 갖고 있으므로 전화걸기를 시도한다.
String str = et_Number.getText().toString();
if (str.length() > 0)
{
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + str));
startActivity(intent);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQCODE_PERMISSION_CALLPHONE)
{
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
btn = findViewById(R.id.btn_call);
((Button) findViewById(R.id.btn_call)).setEnabled(true);
Toast.makeText(this, "권한을 얻었습니다.\n 버튼을 다시 눌러 통화를 시도하세요", Toast.LENGTH_SHORT).show();
}
}
}
}
728x90
반응형
'App > Android Studio' 카테고리의 다른 글
Android Studio - 초 타이머 만들기 (0) | 2022.08.10 |
---|---|
Android Studio - 태스크, 인텐트 플래그 (0) | 2022.08.10 |
Android Studio - 인텐트 필터와 암시적 인텐트 수신 (0) | 2022.08.10 |
프로필 수정 📌미완성 (0) | 2022.08.09 |
Android Studio - 복습 (0) | 2022.08.09 |
댓글