'W/System.err: java.lang.SecurityException: Neither user 10123 nor current process has android.permission.ANSWER_PHONE_CALLS

How can I give permission runtime Answer Phone Calls and If there is another solution for end incoming call please give me

public class CallReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    try {
        String state = intent.getStringExtra(telephonyManager.EXTRA_STATE);
        String incomingNumber = intent.getStringExtra(telephonyManager.EXTRA_INCOMING_NUMBER);

        if (state.equals(telephonyManager.EXTRA_STATE_RINGING)) {
            System.out.println(incomingNumber);
            Toast.makeText(context,"Ringing State Number is - " + incomingNumber, Toast.LENGTH_SHORT).show();
            TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ANSWER_PHONE_CALLS) != PackageManager.PERMISSION_GRANTED) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    telecomManager.endCall();
                }

            }
            else{
                System.out.println("You Don't Have This Permission");
            }
        }

    }
    catch (Exception e){
        e.printStackTrace();
    }


}

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source