End a call in Android

I am trying to end a new outgoing call in a 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:

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

I can’t cancel the outgoing call. I am new to Android and can't get this to work.

+3
source share
4 answers

You cannot end a call in Android. At the moment you are calling startActivity (callIntent), you are giving up control of the caller’s application. Now the call can be canceled only through a telephone request.

, . ComponentName, onReceive . , onReceive, , .

+4

BroadcastReceiver . , 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>

, , . setResultData(null) ​​ , , , .

, , - API PhoneFactory. ( , ), " !". .

+4

( ) .

, 3G- ( ).

+1

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");
+1

Source: https://habr.com/ru/post/1767206/


All Articles