I'm developing an android app which calls a specific contact number, but I need to disconnect the call after some seconds automatically.
Even I need to wait until the above call get disconnected in my application.
I'm developing an android app which calls a specific contact number, but I need to disconnect the call after some seconds automatically.
Even I need to wait until the above call get disconnected in my application.
This was the easiest solution I was able to find for ending a call programatically with a great explanation of it as well and here is an example of how you can end phone call after x seconds:
This would be an example of a method that you could call:
public void startDial(int milliseconds, string phoneNumber){
//performs call
if (!phoneNumber.equals("")) {
Intent intent = new Intent(this, MyReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + milliseconds, pi);
Uri number = Uri.parse("tel:" + phoneNumber);
Intent dial = new Intent(Intent.ACTION_CALL, number);
startActivity(dial, 0);
}
}
And this would be an example of a Receiver you could set up:
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
endCall(context);
}
public void endCall(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
Object telephonyService = m.invoke(tm);
c = Class.forName(telephonyService.getClass().getName());
m = c.getDeclaredMethod("endCall");
m.setAccessible(true);
m.invoke(telephonyService);
} catch (Exception e) {
e.printStackTrace();
}
}
}
For API >= 26 (ANDROID_O)
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
requestPermissions(new String[] {
Manifest.permission.ANSWER_PHONE_CALLS
}, PERMISSION_RQUEST);
TelecomManager mgr = (TelecomManager) getSystemService(TELECOM_SERVICE);
mgr.endCall();