How to make a call programmatically in android

I want to periodically make / receive calls for testing from my Android application and collect statistics from my network. Therefore, my application will call the number so often, and when the call is answered, the application will stop calling after a few seconds. To begin with, the code I understood will work. He will dial and call the number that I will indicate without having to touch the screen.

public class MainActivity extends AppCompatActivity {

int MY_PERMISSIONS_REQUEST_CALL_PHONE = 101;

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

    call();
}

private void call() {

    try {

        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:2125551212"));
        System.out.println("====before startActivity====");



        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) !=
                PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CALL_PHONE},
                    MY_PERMISSIONS_REQUEST_CALL_PHONE);

            return;
        }

        startActivity(callIntent);
        System.out.println("=====getcallActivity==="+getCallingActivity());


    } catch (ActivityNotFoundException e) {
        Log.e("helloAndroid","Call failed",e);
    }
}

}

The manifest has this line:

According to my understanding, ACTION_CALL should put the call on the number that I provided without pressing the DIAL button. But it acts like ACTION_DIAL, which displays the number on the screen, and the user must then press the DIAL button to place the call. So there is no difference between ACTION_DIAL and ACTION_CALL?

, 6,0 , ( )

, Lollipop (5.0) OS, ?

+4
2

ACTION_DIAL

API 1

ACTION_DIAL

. , . , .

. , , getData() - URI , , tel: URI .

: .

: android.intent.action.DIAL


ACTION_CALL

API 1

ACTION_CALL

. -, .

: , ; else getData() - URI , , tel: URI .

: .

  • , ; ACTION_DIAL.
  • . ACTION_DIAL.
  • android M CALL_PHONE , , SecurityException.

: android.intent.action.CALL


( , ):

String number = "7777777777";
Uri call = Uri.parse("tel:" + number);             
Intent surf = new Intent(Intent.ACTION_DIAL, call); 
startActivity(surf);

( android.permission.CALL_PHONE), :

String number = "7777777777";
Uri call = Uri.parse("tel:" + number);             
Intent surf = new Intent(Intent.ACTION_CALL, call); 
startActivity(surf); 
+8

, . , OEM- . , , - android.intent.action.CALL_PRIVILEGED, - android.permission.CALL_PRIVILEGED

ACTION_CALL ACTION_DIAL , .

+1

Source: https://habr.com/ru/post/1664076/


All Articles