问题
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.
回答1:
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.
回答2:
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.
回答3:
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).
回答4:
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");
来源:https://stackoverflow.com/questions/3823987/end-call-in-android