2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【未解決】androidアプリで、着信の自動受話

Last updated at Posted at 2019-06-22

目的

 androidアプリで、電話の着信が来た場合、アプリ側で受話して自動的に通話中にしたい。

環境

android7.1

問題点

TelephonyManagerで、CALL_STATE_RINGING(着信中)の状態で、
 KEYCODE_HEADSETHOOKのキーイベント発生させても、通話状態に遷移しない。

 Android StudioのTerminalから、adb shellでキーイベントを発生させると、
 通話状態に遷移した。

adb shell input keyevent KEYCODE_HEADSETHOOK

adbコマンドでは状態遷移したが、ソフトからは遷移できない。
 権限が不足しているのだろうか?

ソース

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

   final TextView text1 = findViewById(R.id.text);

    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

    telephonyManager.listen(new PhoneStateListener(){
        // PhoneStateListenerの`onCallStateChanged`をOverride
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
                    /* 着信 */
                    text1.setText("Call State Changed: CALL_STATE_RINGING");

                    /* 通話を開始する */
                    dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
                    dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));

                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    /* 通話 */
                    text1.setText("Call State Changed: CALL_STATE_OFFHOOK");
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    /* 待受 */
                    text1.setText("Call State Changed: CALL_STATE_IDLE");
                    break;
            }
        }
    }, PhoneStateListener.LISTEN_CALL_STATE);
}

参考サイト

 「電話着信への応答と拒否」
https://qiita.com/b-wind/items/7e6aac39eefaa9412dee

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?