End Call in Android

心不动则不痛 提交于 2019-12-05 22:26:51

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.

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.

You could enable the Airplane mode (need proper setting) and disable it shortly after.

Of course this would imply a loss of 3G connectivity for a short while (some seconds).

My Solution

Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);              
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
getContext().sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!