.... 살면서 이렇게 조급하게 삽질 해봤을까 싶다.

결론은 기본에 충실해야한다는 것을 깨달았다......

 

 

1. Manifest.xml - Permission 정의

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="패키지명">

    <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/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".MyReceiver"
            android:exported="true"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"></action>
            </intent-filter>
        </receiver>

    </application>

    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    <uses-permission android:name="android.permission.READ_CALL_LOG"></uses-permission>

</manifest>

https://developer.android.com/reference/android/telephony/TelephonyManager#EXTRA_INCOMING_NUMBER

다음의 Android Developer에 명시되어있었다.

이전까지는 READ_PHONE_STATE READ_CALL_LOG 를 permission을 허용해야한다는 것을..... 

 

 

2. MyReceiver.java - Broadcast Receiver 명시

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        // 리시버 상태 체크
        //Toast.makeText(context, "Event !!!", Toast.LENGTH_SHORT).show();

        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
        String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        if (phoneNumber == null ) {
            Log.i("call-state", " : NULL");
        } else {
            if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) {
                Toast.makeText(context, "call Active", Toast.LENGTH_SHORT).show();
            } else if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {
                Toast.makeText(context, "No call", Toast.LENGTH_SHORT).show();
            } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
                Toast.makeText(context, "Ringing State", Toast.LENGTH_SHORT).show();
                Toast.makeText(context, phoneNumber, Toast.LENGTH_SHORT).show();
            }
        }

    }
}

if (phoneNumber == null ) 은 수신이 두 번 호출될 때를 위해서 사용한다.

if문 없이 Toast로 띄워보면 알겠지만, 수신이 2번 호출이 된다.

그래서 처음에는 공백으로, 두번째에 되서야 전화번호를 읽어온다.

그렇기 때문에 처음에 phoneNumber가 공백일때를 처음 if문 처리를 해주면 된다.

 

 

3. 앱에서의 Permission 허용하기

다음과 같이 실제로 앱에 permission을 줘야한다.

직접적으로 이렇게 눌러서 권한을 주는 방법도 있지만,

 

 

선호하는 방법은 사용자에게 권한을 부여해달라고 직접 코드를 짜는 것 이다. (사용자에게 편리하도록..ㅋㅋ)

사용자에게 권한을 주는 코드는 다음과 같다.

 

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(MainActivity.this
                    , new String[]{Manifest.permission.READ_CALL_LOG
                            , Manifest.permission.READ_PHONE_STATE}
                            ,1);
        }


    }
}

 

 

폰넘버가 뭐라고 이게 이렇게 어려웠을까.....

ㅠㅠ 역시 게을렀기 때문이라고 한다....ㅎ

 

 

+ Recent posts