我可以挂断呼叫编程的机器人?机器人

2023-09-03 21:29:51 作者:贱人必有天收

我希望能阻止某些曲柄调用,所以我需要以编程方式挂断电话的一些android系统中。

I hope to block some crank call, so I need to hang up some call programmatically in android.

下面的片段是从如何挂在Android的呼出? 这是否意味着挂断电话将挡在进一步的Andr​​oid版本的任何时间的技术? 这是否意味着我不能写一个应用程序挂了个电话吗?

The following fragment is from How to hang up outgoing call in Android? Does it mean the technique of hang up call will be block at any time in further Android version? Does it mean I can't write an app to hang up a call?

挂断了我到目前为止遇到的唯一途径,就是通过Java反射来这样做。因为它不是公共API的一部分,你应该小心使用它,而不是依赖于它。任何更改到Android的内部结构将有效地打破你的应用程序。的

推荐答案

首先,你需要在的Andr​​oidManifest.xml

<uses-permission android:name="android.permission.CALL_PHONE" />

然后你设置了一个BroadcastReceiver服务 android.intent.action.PHONE_STATE (来电)和 android.intent.action.NEW_OUTGOING_CALL (拨出电话)加入以下为的Andr​​oidManifest.xml

Then you setup a BroadcastReceiver service for android.intent.action.PHONE_STATE (incoming calls) and android.intent.action.NEW_OUTGOING_CALL (outgoing calls) by adding the following to AndroidManifest.xml

AndroidManifest.xml中

<receiver android:name=".PhoneStateReceiver">
    <intent-filter android:priority="0">
        <action android:name="android.intent.action.PHONE_STATE" />
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
    </intent-filter>
</receiver>

PhoneStateReceiver.JAVA

import java.lang.reflect.Method;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;

public class PhoneStateReceiver extends BroadcastReceiver {

    public static String TAG="PhoneStateReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.PHONE_STATE")) { 
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            Log.d(TAG,"PhoneStateReceiver**Call State=" + state);

            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                Log.d(TAG,"PhoneStateReceiver**Idle");
            } else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
                // Incoming call
                String incomingNumber = 
                        intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                Log.d(TAG,"PhoneStateReceiver**Incoming call " + incomingNumber);

                if (!killCall(context)) { // Using the method defined earlier
                    Log.d(TAG,"PhoneStateReceiver **Unable to kill incoming call");
                }

            } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                Log.d(TAG,"PhoneStateReceiver **Offhook");
            }
        } else if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) { 
            // Outgoing call
            String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            Log.d(TAG,"PhoneStateReceiver **Outgoing call " + outgoingNumber);

            setResultData(null); // Kills the outgoing call

        } else {
            Log.d(TAG,"PhoneStateReceiver **unexpected intent.action=" + intent.getAction());
        }
    }

    public boolean killCall(Context context) {
        try {
            // Get the boring old TelephonyManager
            TelephonyManager telephonyManager =
                    (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

            // Get the getITelephony() method
            Class classTelephony = Class.forName(telephonyManager.getClass().getName());
            Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");

            // Ignore that the method is supposed to be private
            methodGetITelephony.setAccessible(true);

            // Invoke getITelephony() to get the ITelephony interface
            Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);

            // Get the endCall method from ITelephony
            Class telephonyInterfaceClass =  
                    Class.forName(telephonyInterface.getClass().getName());
            Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");

            // Invoke endCall()
            methodEndCall.invoke(telephonyInterface);

        } catch (Exception ex) { // Many things can go wrong with reflection calls
            Log.d(TAG,"PhoneStateReceiver **" + ex.toString());
            return false;
        }
        return true;
    }

}