3

我是android的新手。我写这段代码是为了打电话

Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + bundle.getString("mobilePhone")));
context.startActivity(intent);

我知道有一个侦听器可以使用此代码查找手机的状态。

  private PhoneStateListener mPhoneListener = new PhoneStateListener() {
  public void onCallStateChanged(int state, String incomingNumber) {
  try {
  switch (state) {
  case TelephonyManager.CALL_STATE_RINGING:
  Toast.makeText(CaptureCall.this, "CALL_STATE_RINGING", Toast.LENGTH_SHORT).show();
  break;
  case TelephonyManager.CALL_STATE_OFFHOOK:
  Toast.makeText(CaptureCall.this, "CALL_STATE_OFFHOOK", Toast.LENGTH_SHORT).show();
  break;
  case TelephonyManager.CALL_STATE_IDLE:
  Toast.makeText(CaptureCall.this, "CALL_STATE_IDLE", Toast.LENGTH_SHORT).show();
  break;
  default:
  Toast.makeText(CaptureCall.this, "default", Toast.LENGTH_SHORT).show();
  Log.i("Default", "Unknown phone state=" + state);
   }
  } catch (Exception e) {
   Log.i("Exception", "PhoneStateListener() e = " + e);
  }
 }
};

实际上,我拨打第一个号码并听取电话的状态。如果电话状态变为IDLE我拨打第二个号码。但它不起作用。

任何人请建议我如何完成这项任务。

4

2 回答 2

0

您可以使用以下方式拨打第一个号码吗?

       Intent intent = new Intent(Intent.ACTION_CALL);
       intent.setData(Uri.parse("tel:" + bundle.getString("mobilePhone")));
       context.startActivity(intent);

如果没有,也添加以下内容。

       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

并使用相同的代码拨打第二个电话号码以拨打处于空闲状态的第一个号码。

但请注意,即使在您开始呼叫第一个号码之前,IDLE 状态也会为真,在这种情况下,您的第二个号码将首先被呼叫。在空闲状态下,如果第一个号码被呼叫,您可以设置一个标志,然后开始呼叫第二个号码。

希望能帮助到你。

于 2013-12-04T16:40:28.350 回答
0

如果我正确理解您的问题,您正在尝试在完成之前的通话后拨打号码。

这是顺序调用n个数字的算法

  1. 制作要呼叫的号码队列
  2. 为 PhoneState 创建和注册广播接收器
  3. 出列元素并触发调用意图
  4. 在第 2 点创建的广播接收器将侦听空闲状态的呼叫丢弃状态,
  5. 如果队列中有更多元素,请重复步骤 3-4
  6. 否则,取消注册广播接收器。

C# 代码片段:

                Queue phoneNumbers = new Queue();


                foreach (var contact in emergencyContacts)
                    phoneNumbers.Enqueue(contact.contactNo);

                ShareUtils.CallToNumber(context, phoneNumbers.Dequeue().ToString());
                var callReceiver = new CallReceiver();
                callReceiver.onReceive += (intent) =>
                {
                    if (intent.Extras.GetString(TelephonyManager.ExtraState).Equals(TelephonyManager.ExtraStateIdle))
                    {
                        if (phoneNumbers.Count > 0)
                        {
                            var phNumber = phoneNumbers.Dequeue().ToString();
                            ShareUtils.CallToNumber(context, phNumber);
                            Toast.MakeText(this.Activity, $"Calling {phNumber}", ToastLength.Long).Show();
                        }
                        else
                        {
                            context.UnregisterReceiver(callReceiver);
                            ((LandingActivityNew)this.Activity)?.LoadSOS_SuccessFragment();
                        }
                    }
                };
                context.RegisterReceiver(callReceiver, new IntentFilter("android.intent.action.PHONE_STATE"));
于 2017-06-08T15:48:54.210 回答