【问题标题】:End Call in Android在 Android 中结束通话
【发布时间】:2010-09-29 16:56:53
【问题描述】:

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

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 新手,似乎无法使其正常工作。

【问题讨论】:

    标签: android android-intent telephony


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      BroadcastReceiver 类应该拦截所有调用。检查接收器类是否在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>
      

      也就是说,我认为在接收器函数完成之后 取消调用是不可能的。我已经尝试将setResultData(null) 移动到一个延迟几秒钟的线程中,但我认为这没有效果的原因是因为意图已经执行并且调用已经开始。

      实现这一点的唯一方法是通过PhoneFactory 中的内部 API 调用弄脏您的双手。有很多尝试这样做(据我所知没有成功),并且有一个响亮的“不要这样做!”来自社区。​​p>

      【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        我的解决方案

        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");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-05-19
          • 1970-01-01
          • 2011-08-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多