使用telephonyserivce.endcall()在android中结束调用

时间:2012-12-30 15:57:11

标签: android telephony telephonymanager

我正在开发应用,我希望结束拨出电话。 这是主要的课程

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);
}
}
} 

和outgoingclass就是这个

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);
    }       

    }
}

但即使我能打印否,呼叫也没有结束。当拨出电话开始时,表明广播接收机工作正常。

任何建议

1 个答案:

答案 0 :(得分:0)

肯定有两种方法可以使用飞机模式或内部电话来实现。我使用内部电话,我开发的代码在这里上传https://github.com/deependersingla/end_calls

如果您需要更多信息告诉我,我们将很乐意为您提供帮助。