Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

End Call in Android

I'm trying to end a new outgoing call after few seconds. Here is my code:

public class phonecalls extends Activity {
    ComponentName cn;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        call();
    }
    private void call() {
        try {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:123456789"));
            startActivity(callIntent);
            cn = new ComponentName(getApplicationContext(), Receiver.class);
        } catch (ActivityNotFoundException activityException) {
            Log.e("dialing-example", "Call failed", activityException);
        }
    }
}

My receiver class is:

public class Receiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            setResultData(null);
        }
    }
}

I am unable to abort the outgoing call. I'm new to Android and can't seem to get this to work.

like image 879
user458295 Avatar asked Jul 23 '26 07:07

user458295


2 Answers

You can't end a call in Android. At the moment you are calling startActivity(callIntent) you are giving up control to the native caller application. The call can now only be aborted through the phone application.

Besides that your code after starting the call does not make much sense. Your are not doing anything with the ComponentName that your are creating, so the onReceive method of your Receiver will never be called. Also if the receiver would be called the code in the onReceive method won't do anything to alter the state of the phone call.

like image 62
Janusz Avatar answered Jul 25 '26 19:07

Janusz


The BroadcastReceiver class should be intercepting all calls. Check if the receiver class is registered correctly in AndroidManifest.xml:

<receiver android:name="Receiver" android:exported="true" android:enabled="true">
    <intent-filter android:priority="1">
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        <action android:name="android.intent.action.PHONE_STATE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

That said, I don't believe that it's possible to cancel the call after the receiver function completes. I've tried moving setResultData(null) into a thread with a few seconds delay, but I believe the reason this has no effect is because the intent has already been executed and the call has been started.

The only way that this might be possible is getting your hands dirty with internal API calls in PhoneFactory. There have been many attempts to do this (to my knowledge without success), and there is a resounding "do not do this!" from the community.

like image 35
Paul Lammertsma Avatar answered Jul 25 '26 19:07

Paul Lammertsma