I want to make/receive calls periodically for testing purposes from my android app programmatically and collects stats from my network. So my app will call a number every so often and when the call is answered the app will terminate the call after a few seconds. TO begin with here is the code I understood would work. It will dial and a call the number I specify without me 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:
As per my understanding ACTION_CALL should place the call to the number I have provided without having to press the DIAL button. But is acting like ACTION_DIAL, which displays the number on the screen and the user then has to press DIAL button to place the call. So is there no difference between ACTION_DIAL and ACTION_CALL?
After reading some of the posts I understand that 6.0 onwards permission is needs to be requested from the user to dial the call(which I have used in my code above)
My question is if I use Lollipop(5.0) OS then will I be able to place the call without dialing?
ACTION_DIAL
Added in API level 1
String ACTION_DIAL
Activity Action: Dial a number as specified by the data. This shows a UI with the number being dialed, allowing the user to explicitly initiate the call.
Input: If nothing, an empty dialer is started, else getData()
is URI of a phone number to be dialed or a tel: URI of an explicit phone number.
Output: nothing.
Constant Value: android.intent.action.DIAL
ACTION_CALL
Added in API level 1
String ACTION_CALL
Activity Action: Perform a call to someone specified by the data.
Input: If nothing, an empty dialer is started; else getData()
is URI of a phone number to be dialed or a tel: URI of an explicit phone number.
Output: nothing.
Note:
ACTION_DIAL
.ACTION_DIAL
.CALL_PHONE
permission which is not granted, then attempting to use this action
will result in a SecurityException.Constant Value: android.intent.action.CALL
so basically
To just open the dialer app (the user has to press the call button inside the dialer app; no additional permissions needed) use:
String number = "7777777777";
Uri call = Uri.parse("tel:" + number);
Intent surf = new Intent(Intent.ACTION_DIAL, call);
startActivity(surf);
To open the dialer app and do the call automatically (needs android.permission.CALL_PHONE
) and then use:
String number = "7777777777";
Uri call = Uri.parse("tel:" + number);
Intent surf = new Intent(Intent.ACTION_CALL, call);
startActivity(surf);