2013-04-21 43 views
3

我新手android.i寫代碼撥打電話如何以編程方式一個接一個地打3個電話?

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,我撥打第二個號碼。但它不起作用。

任何一個PLZ建議我如何做到這一點。

+1

我們不知道什麼是 「實在不行」 的意思。更加詳細一些。 – 2013-04-21 07:29:00

回答

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狀態下的第一個號碼。

但請注意,即使在您開始撥打第一個號碼之前,IDLE狀態仍然爲真,在這種情況下,您的第二個號碼將首先被呼叫。在IDLE狀態下,如果調用第一個數字,則可以設置一個標誌,然後開始調用第二個數字。

希望它有幫助。

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")); 
相關問題