如何在启动 activity 后中止通话并关闭拨号程序?

How do I abort a call and close the dialer after launching my activity?

据我所知,Avast 防盗功能让您可以设置一个 "secret pin",您可以拨打该 "secret pin" 以打开 UI。当我拨号时发生的事情是 呼叫没有通过 ,然后拨号程序立即关闭并且 UI 打开(或者 UI 超过拨号器 activity。我忘了)。

我以某种方式实现了这个,但它并没有中止调用,而是进入后台(绿色状态栏、铃声 phone 等),而我的 activity 则在它前面。

如何在拨打特定号码时打开自己的 activity 来中止通话?

关于我的方法的一些代码:

呼叫的广播接收器:

public class OutgoingCallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        // Match if action is outgoing call.

            Intent i = new Intent();
            i.setAction(HelperClass.ACTION_NEW_OUTGOINGCALL);
            i.setClassName(HelperClass.PACKAGE_NAME, HelperClass.CLASS_NAME_OUTGOING);
            i.putExtra(HelperClass.CONTACT_KEY, "contact");
            i.putExtra(HelperClass.CLIENT_DEVICE_KEY, "DUMMY");
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            startActivity(i);

            // Apparently, this doesn't work
            abortBroadcast();





// --------------- FAILED ATTEMPT ---------------------

//        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
//            TelephonyManager tm = (TelephonyManager) context
//                    .getSystemService(Context.TELEPHONY_SERVICE);
//            try {
//                // Java reflection to gain access to TelephonyManager's
//                // ITelephony getter
//                Log.v(HelperClass.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);
//            }
        }
    }
}

尝试失败:未找到 ITelephony。失败的尝试来自

更新:尝试实施,失败。不确定如何实施:How to hang up outgoing call in Android?

还查看了:How to abort an outgoing call after fixed seconds?

更新:将 setResultData(null); 添加到广播接收器有效。 它立即结束了通话。

样本:

public class OutgoingCallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Immediately cancels the call since this sets the number to null.
        setResultData(null);

        // Launch activity here/other stuff you want to do
        // Example:
        //     Intent i = new Intent(context, MyActivity.class);
        //     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //     context.startActivity(i);
    }
}

清单:

...
        <receiver
            android:name=".OutgoingCallReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="2147483647">
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>
...