语音通话开始时如何开始?

How to start something when voice call starts?

我已经构建了完整的录音机应用程序。

我想在 phone 上开始语音通话时开始录音,如何检测通话状态?尝试了一些代码,但它对我不起作用。

我只需要知道语音通话开始(呼入和呼出)时开始录音即可。

Here 是您需要的示例。

在AndroidManifest

中声明接收者
<receiver android:name=".IncomingCall">   
            <intent-filter>
               <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
    </receiver>

在 AndroidManifest

中授予读取 phone 状态权限
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

创建一个 class IncomingCall with extends BroadcastReceiver class

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by matheszabi on Aug/20/2017 0020.
 */

public class IncomingCall extends BroadcastReceiver {

    private Context context;

    public void onReceive(Context context, Intent intent) {

        this.context = context;
        try {
            // TELEPHONY MANAGER class object to register one listner
            TelephonyManager tmgr = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);

            //Create Listner
            MyPhoneStateListener PhoneListener = new MyPhoneStateListener();

            // Register listener for LISTEN_CALL_STATE
            tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

        } catch (Exception e) {
            Log.e("Phone Receive Error", " " + e);
        }

    }

    private class MyPhoneStateListener extends PhoneStateListener {

        public void onCallStateChanged(int state, String incomingNumber) {

            Log.d("MyPhoneListener",state+"   incoming no:"+incomingNumber);

            if (state == 1) {

                String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(context, msg, duration);
                toast.show();

            }
        }
    }
}

高于 Android 6.0 你需要处理一些不同的权限:

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final int MY_REQUEST_CODE = 1234;

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



    @Override
    protected void onResume() {
        super.onResume();

        if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED) {

            requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
                    MY_REQUEST_CODE);
        }
    }


    public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == MY_REQUEST_CODE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Now user should be able to use camera
                Toast.makeText(this, "I have  access", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "I DON'T have  access", Toast.LENGTH_SHORT).show();
            }


        }
    }
}

您必须在第一次允许权限运行:

这是工作代码的屏幕截图:

我找到了方法:

package com.example.tsuryohananov.mycallrecorder;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by tsuryohananov on 20/08/2017.
 */

public class MyPhoneReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();

        if (extras != null) {
            String state = extras.getString(TelephonyManager.EXTRA_STATE);
            Log.d("MY_DEBUG_TAG", state);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                Log.d("MY_DEBUG_TAG", phoneNumber);
                // here i need to save the number for the listview.
            }
            if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))){
                String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                Toast.makeText(context,"Answered" + phoneNumber, Toast.LENGTH_SHORT).show();
                MainActivity.recordStart();
            }
            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                Toast.makeText(context,"Idle State", Toast.LENGTH_SHORT).show();
                MainActivity.stopRecord();
            }
        }
    }
}