如何在Android中挂断电话?

Til*_*lek 45 android telephony telephonymanager

我正在开发一个应用程序,其中我们需要的一件事是控制传出呼叫,至少能够从我们的应用程序中阻止它.

我尝试过使用Intent.ACTION_CALL现有的活动:

Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber)); 
startActivity(callIntent); 
Run Code Online (Sandbox Code Playgroud)

但是通过API似乎不允许停止呼叫.

你能建议一些解决方法吗?

例如:在通话期间启用飞机模式?只是一个例子; 这个黑客对我不起作用.

Pau*_*sma 31

BroadcastReceiver已经提到了在a中捕获拨出呼叫,如果你想在拨号之前结束通话,这绝对是最好的方法.

但是,一旦拨号或通话,该技术就不再适用.我到目前为止挂断的唯一方法是通过Java Reflection完成.由于它不是公共API的一部分,因此您应该小心使用它,而不是依赖它.对Android内部组成的任何更改都将有效地破坏您的应用程序.

Prasanta Paul的博客演示了如何实现它,我在下面总结了这一点.

获取ITelephony对象:

TelephonyManager tm = (TelephonyManager) context
        .getSystemService(Context.TELEPHONY_SERVICE);
try {
    // Java reflection to gain access to TelephonyManager's
    // ITelephony getter
    Log.v(TAG, "Get getTeleService...");
    Class c = Class.forName(tm.getClass().getName());
    Method m = c.getDeclaredMethod("getITelephony");
    m.setAccessible(true);
    com.android.internal.telephony.ITelephony telephonyService =
            (ITelephony) m.invoke(tm);
} catch (Exception e) {
    e.printStackTrace();
    Log.e(TAG,
            "FATAL ERROR: could not connect to telephony subsystem");
    Log.e(TAG, "Exception object: " + e);
}
Run Code Online (Sandbox Code Playgroud)

结束通话:

telephonyService.endCall();
Run Code Online (Sandbox Code Playgroud)

  • +1不是很好的做法,但是OP专门针对API不允许的方法提出了解决方法,因此这是一个很好的答案. (6认同)

Fel*_*ipe 26

试试这个:

(我使用Reflection来访问高级电话功能并修改某些内容)

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

try {
    //String serviceManagerName = "android.os.IServiceManager";
    String serviceManagerName = "android.os.ServiceManager";
    String serviceManagerNativeName = "android.os.ServiceManagerNative";
    String telephonyName = "com.android.internal.telephony.ITelephony";

    Class telephonyClass;
    Class telephonyStubClass;
    Class serviceManagerClass;
    Class serviceManagerStubClass;
    Class serviceManagerNativeClass;
    Class serviceManagerNativeStubClass;

    Method telephonyCall;
    Method telephonyEndCall;
    Method telephonyAnswerCall;
    Method getDefault;

    Method[] temps;
    Constructor[] serviceManagerConstructor;

    // Method getService;
    Object telephonyObject;
    Object serviceManagerObject;

    telephonyClass = Class.forName(telephonyName);
    telephonyStubClass = telephonyClass.getClasses()[0];
    serviceManagerClass = Class.forName(serviceManagerName);
    serviceManagerNativeClass = Class.forName(serviceManagerNativeName);

    Method getService = // getDefaults[29];
    serviceManagerClass.getMethod("getService", String.class);

    Method tempInterfaceMethod = serviceManagerNativeClass.getMethod(
                "asInterface", IBinder.class);

    Binder tmpBinder = new Binder();
    tmpBinder.attachInterface(null, "fake");

    serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
    IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
    Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);

    telephonyObject = serviceMethod.invoke(null, retbinder);
    //telephonyCall = telephonyClass.getMethod("call", String.class);
    telephonyEndCall = telephonyClass.getMethod("endCall");
    //telephonyAnswerCall = telephonyClass.getMethod("answerRingingCall");

    telephonyEndCall.invoke(telephonyObject);

} catch (Exception e) {
    e.printStackTrace();
    Log.error(DialerActivity.this,
                "FATAL ERROR: could not connect to telephony subsystem");
    Log.error(DialerActivity.this, "Exception object: " + e);
}
Run Code Online (Sandbox Code Playgroud)

  • 效果很好.[必需权限"<uses-permission android:name ="android.permission.CALL_PHONE"/>"] (3认同)

小智 21

  1. 创建BroadcastReceiver优先级为0的.
  2. 在BC中拦截ACTION_NEW_OUTGOING_CALLonReceive方法的意图
  3. setResultData(null)用同样的方法打电话

这将阻止调用启动(只要您的接收者是最后一个处理我认为的意图)

  • 我可以验证这适用于Android 2.1和2.2. (2认同)

and*_*per 9

这是最新的代码,它也适用于 Android P,因为它有一个官方 API(这里):

在清单中,添加以下内容:

<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS"/>
Run Code Online (Sandbox Code Playgroud)

在代码中,使用这个:

爪哇:

@SuppressLint("PrivateApi")
public static boolean endCall(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        final TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
        if (telecomManager != null && ContextCompat.checkSelfPermission(context, Manifest.permission.ANSWER_PHONE_CALLS) == PackageManager.PERMISSION_GRANTED) {
            telecomManager.endCall();
            return true;
        }
        return false;
    }
    //use unofficial API for older Android versions, as written here: /sf/answers/586629291/
    try {
        final Class<?> telephonyClass = Class.forName("com.android.internal.telephony.ITelephony");
        final Class<?> telephonyStubClass = telephonyClass.getClasses()[0];
        final Class<?> serviceManagerClass = Class.forName("android.os.ServiceManager");
        final Class<?> serviceManagerNativeClass = Class.forName("android.os.ServiceManagerNative");
        final Method getService = serviceManagerClass.getMethod("getService", String.class);
        final Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class);
        final Binder tmpBinder = new Binder();
        tmpBinder.attachInterface(null, "fake");
        final Object serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
        final IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
        final Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);
        final Object telephonyObject = serviceMethod.invoke(null, retbinder);
        final Method telephonyEndCall = telephonyClass.getMethod("endCall");
        telephonyEndCall.invoke(telephonyObject);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        LogManager.e(e);
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

or in Kotlin:

@SuppressLint("PrivateApi")
fun endCall(context: Context): Boolean {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        val telecomManager = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.ANSWER_PHONE_CALLS) == PackageManager.PERMISSION_GRANTED) {
            telecomManager.endCall()
            return true
        }
        return false
    }
    //use unofficial API for older Android versions, as written here: /sf/answers/586629291/
    try {
        val telephonyClass = Class.forName("com.android.internal.telephony.ITelephony")
        val telephonyStubClass = telephonyClass.classes[0]
        val serviceManagerClass = Class.forName("android.os.ServiceManager")
        val serviceManagerNativeClass = Class.forName("android.os.ServiceManagerNative")
        val getService = serviceManagerClass.getMethod("getService", String::class.java)
        val tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder::class.java)
        val tmpBinder = Binder()
        tmpBinder.attachInterface(null, "fake")
        val serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder)
        val retbinder = getService.invoke(serviceManagerObject, "phone") as IBinder
        val serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder::class.java)
        val telephonyObject = serviceMethod.invoke(null, retbinder)
        val telephonyEndCall = telephonyClass.getMethod("endCall")
        telephonyEndCall.invoke(telephonyObject)
        return true
    } catch (e: Exception) {
        e.printStackTrace()
        return false
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以尝试启用然后禁用飞行模式:

android.provider.Settings.System.putInt(getContentResolver(),
        android.provider.Settings.System.AIRPLANE_MODE_ON, 1);

Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", 1);
sendBroadcast(new Intent("android.intent.action.AIRPLANE_MODE"));
sendBroadcast(intent);
android.provider.Settings.System.putInt(getContentResolver(),
        android.provider.Settings.System.AIRPLANE_MODE_ON, 0);

intent.putExtra("state", 0);
sendBroadcast(new Intent("android.intent.action.AIRPLANE_MODE"));
sendBroadcast(intent);
Run Code Online (Sandbox Code Playgroud)

  • 大锤,遇见坚果. (11认同)
  • 此时没有办法挂断电话,本身,Gung Shi Jie提出的解决方案是一个好主意,但不起作用,在拨打电话时,状态"AIRPLANE MODE"的更改将被忽略,其工作原理只有在开发过程中的emulaterd终端上,我已经尝试过在HTC Desire和Acer Liquid手机上都失败了. (4认同)

Pat*_*Pat 3

考虑到可能造成奇妙恶作剧的可能性,如果允许这样做,我会感到惊讶。

该线程直截了当地说API 无法结束调用。其他人已经尝试过

  • 那么他们在[tCallBlocking Lite](http://www.taosoftware.co.jp/en/android/callblockinglite/)中是如何做到的呢? (3认同)