我可以在android中以编程方式挂断电话吗?

时间:2014-04-16 02:29:21

标签: android

我希望阻止一些曲柄调用,所以我需要在android中以编程方式挂断一些调用。

以下片段来自How to hang up outgoing call in Android?
这是否意味着挂断电话的技术会在进一步的Android版本中随时被阻止? 这是否意味着我无法编写应用程序来挂断电话?

到目前为止我遇到的唯一方法就是通过Java Reflection这样做。由于它不是公共API的一部分,因此您应该小心使用它,而不是依赖它。对Android内部组成的任何更改都将有效地破坏您的应用程序。

5 个答案:

答案 0 :(得分:72)

首先,您需要在AndroidManifest.xml

中声明此权限
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

然后,您通过将以下内容添加到BroadcastReceiver

,为android.intent.action.PHONE_STATE(来电)和android.intent.action.NEW_OUTGOING_CALL(拨出电话)设置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;
    }

}

MainActivity.JAVA

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

public static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_PHONE_STATE)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.READ_PHONE_STATE)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_PHONE_STATE},
                    MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);

            // MY_PERMISSIONS_REQUEST_READ_PHONE_STATE is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay!

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}
}

答案 1 :(得分:2)

Does it mean the technique of hang up call will be block at any time in further Android version?

Android团队有可能将所有内容颠倒过来为这项具体服务做好准备。至于我,我认为这种可能性几乎可以忽略不计。

Does it mean I can't write an app to hang up a call?

如果提议的方法有效,为什么不呢?只要这样做,记住(在你的思想背景中的某个地方,是的:))在遥远的未来某个地方可能会变得复杂,你必须寻找另一个肮脏的黑客突破不生锈的API,不允许你挂电话。

答案 2 :(得分:1)

这完全没有经过测试,但是当你的黑名单出现与之相符的电话时,你是否只能关闭飞机模式?

除此之外,这似乎适用于pre kitkat http://androidsourcecode.blogspot.in/2010/10/blocking-incoming-call-android.html

答案 3 :(得分:1)

更改 android API >= 26 (ANDROID_O) 后,其他答案不起作用

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

然后

requestPermissions(new String[] {
   Manifest.permission.ANSWER_PHONE_CALLS
 }, PERMISSION_RQUEST);

然后

TelecomManager mgr = (TelecomManager) getSystemService(TELECOM_SERVICE);
mgr.endCall();

答案 4 :(得分:0)

如果您正在进行JUnit测试,那么就是这样;

kafka-acls.sh --add --allow-principals user:ctadmin --operation ALL --topic marchTesting --authorizer-properties zookeeper.connect={hostname}:2181

它也可以在测试之外工作,但我还没有尝试过。