3

我正试图在几秒钟后结束新的拨出电话。这是我的代码:

public class phonecalls extends Activity {
    ComponentName cn;
    /** 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);
            cn = new ComponentName(getApplicationContext(), Receiver.class);
        } catch (ActivityNotFoundException activityException) {
            Log.e("dialing-example", "Call failed", activityException);
        }
    }
}

我的接收器类是:

public class Receiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            setResultData(null);
        }
    }
}

我无法中止拨出电话。我是 Android 新手,似乎无法使其正常工作。

4

4 回答 4

4

您无法在 Android 中结束通话。在您调用 startActivity(callIntent) 的那一刻,您正在放弃对本机调用者应用程序的控制。现在只能通过电话应用程序中止呼叫。

除此之外,您在开始通话后的代码没有多大意义。您没有对您正在创建的 ComponentName 做任何事情,因此您的 Receiver 的 onReceive 方法将永远不会被调用。此外,如果接收者将被调用,onReceive 方法中的代码将不会做任何事情来改变电话呼叫的状态。

于 2011-01-26T11:47:33.110 回答
4

The BroadcastReceiver class should be intercepting all calls. Check if the receiver class is registered correctly in AndroidManifest.xml:

<receiver android:name="Receiver" android:exported="true" android:enabled="true">
    <intent-filter android:priority="1">
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        <action android:name="android.intent.action.PHONE_STATE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

That said, I don't believe that it's possible to cancel the call after the receiver function completes. I've tried moving setResultData(null) into a thread with a few seconds delay, but I believe the reason this has no effect is because the intent has already been executed and the call has been started.

The only way that this might be possible is getting your hands dirty with internal API calls in PhoneFactory. There have been many attempts to do this (to my knowledge without success), and there is a resounding "do not do this!" from the community.

于 2011-01-26T11:50:41.113 回答
1

您可以启用飞行模式(需要正确设置)并在不久之后将其禁用。

当然,这意味着会在短时间内(几秒钟)失去 3G 连接。

于 2011-02-13T07:34:49.600 回答
1

我的解决方案

Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);              
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
getContext().sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
于 2012-04-27T05:14:45.953 回答