Skip to content Skip to sidebar Skip to footer

Endcall() Function Dosen't Work

My Manifest.xml: I could capt

Solution 1:

Try to use the headset functions like this:

public class CallReceiver extends BroadcastReceiver {
...
    try {
        endCallAidl(context);
    }
    catch (Exception e) {
        e.printStackTrace();
        Log.d("EndCall","Error trying to end call using telephony service.  Falling back to headset.");
            endPhoneHeadsethook(context);
    } 
...
}

private void endPhoneHeadsethook(Context context) {
Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
    headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
    String origState = headSetUnPluggedintent.getStringExtra("state");
    headSetUnPluggedintent.putExtra("state", 1);
    headSetUnPluggedintent.putExtra("name", "Headset");

    // TODO: Should we require a permission?
    sendOrderedBroadcast(headSetUnPluggedintent, null);

    Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);             
    buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
    getBaseContext().sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");


    // froyo and beyond trigger on buttonDown instead of buttonUp
    Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);             
    buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
    context.sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED");

    if (origState != "1"){
        headSetUnPluggedintent.putExtra("state", 0);
        sendOrderedBroadcast(headSetUnPluggedintent, null);
    } 
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private void endCallAidl(Context context) throws Exception {

        // Set up communication with the telephony service (thanks to Tedd's Droid Tools!)
        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        Class c = Class.forName(tm.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        ITelephony telephonyService;
        telephonyService = (ITelephony)m.invoke(tm);

        // end the call!
        telephonyService.endCall(); 
}

Post a Comment for "Endcall() Function Dosen't Work"