ending call in android using telephonyserivce.endc

2019-05-14 17:49发布

问题:

I am working on app where i want to end outgoing call. this is the main class

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class phonecalls extends Activity {
/** 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);
} catch (ActivityNotFoundException activityException) {
    Log.e("dialing-example", "Call failed", activityException);
}
}
} 

and outgoingclass is this

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
import com.android.internal.telephony.ITelephony;
import java.lang.reflect.Method;

public class OutgoingCallReceiver extends BroadcastReceiver {

private final String TAG = "CallControl";
Context context;
String incomingNumber;
ITelephony telephonyService;  

@Override
    public void onReceive(Context context, Intent intent) {
    TelephonyManager tm = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE);
    try {
        // Java reflection to gain access to TelephonyManager's
        // ITelephony getter
        Bundle bundle = intent.getExtras();

        if(null == bundle)
                return;

        String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

        Log.i("OutgoingCallReceiver",phonenumber);
        Log.i("OutgoingCallReceiver",bundle.toString());

        String info = "Detect Calls sample application\nOutgoing number: " + phonenumber;


        Toast.makeText(context, info, Toast.LENGTH_LONG).show();

        Log.v(TAG, "Get getTeleService...");
        Class c = Class.forName(tm.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        telephonyService=(ITelephony) m.invoke(tm);
        telephonyService.endCall();
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG,
                "FATAL ERROR: could not connect to telephony subsystem");
        Log.e(TAG, "Exception object: " + e);
    }       

    }
}

But the call is not ended even though i am able to print no. when outgoing call starts which show broadcasting receiver is working right .

Any suggestion

回答1:

Yes definately there is two way to do it either using airplane mode or internal telephony for this . I used internal telephony and code I developed is uploaded here https://github.com/deependersingla/end_calls.

If you need anything more tell me , will be happy to help.