2013-10-04 81 views
1

我已經創建了應用程序,用戶可以在按鈕點擊時撥打電話。每次結束電話即使我沒有打電話給我的應用程序,我的電話仍然會回到我的應用程序android

我發現下面的代碼是正常工作,撥打電話,回到我的活動時,通話結束。但是我在這個應用程序中遇到了一個問題,那就是一旦我從應用程序撥打電話並結束該通話,在完成整個週期後,我按下主頁按鈕或返回按鈕。我會從我的電話目錄撥打一個電話號碼,當結束通話時,它會回到我的應用程序而不是電話號碼簿。

public void imgbtnCallPhone_Click(View view) { 
    EditText txtBusinessPhone = (EditText) findViewById(R.id.txtPhone); 

     try 
     { 
      final Intent callIntent = new Intent(android.content.Intent.ACTION_CALL); 
      callIntent.setData(Uri.parse("tel:"+ txtBusinessPhone.getText())); 
      startActivity(callIntent); 
     } catch (ActivityNotFoundException activityException) { 
      //Log.e("Calling a Phone Number", "Call failed", activityException); 
     } 

} 

private class PhoneCallListener extends PhoneStateListener { 

     private boolean isPhoneCalling = false; 

     String LOG_TAG = "LOGGING 123"; 

     @Override 
     public void onCallStateChanged(int state, String incomingNumber) { 

      if (TelephonyManager.CALL_STATE_RINGING == state) { 
       // phone ringing 
       //Log.i(LOG_TAG, "RINGING, number: " + incomingNumber); 
      } 

      if (TelephonyManager.CALL_STATE_OFFHOOK == state) { 
       // active 
       //Log.i(LOG_TAG, "OFFHOOK"); 

       isPhoneCalling = true; 
      } 

      if (TelephonyManager.CALL_STATE_IDLE == state) { 
       // run when class initial and phone call ended, 
       // need detect flag from CALL_STATE_OFFHOOK 
       //Log.i(LOG_TAG, "IDLE"); 

       if (isPhoneCalling) { 

        //Log.i(LOG_TAG, "restart app"); 

        // restart app 
        Intent i = getBaseContext().getPackageManager() 
         .getLaunchIntentForPackage(
          getBaseContext().getPackageName()); 
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(i); 

        isPhoneCalling = false; 
       } 

      } 
     } 
    } 

我想,會檢查電話是有關我的應用程序或沒有, 如果我的應用程序進行電話呼叫,然後結束後電話撥打它會回來 我的應用程序的活動,否則做不成碼回到我的活動,做它默認。

感謝,

回答

1

我認爲你需要啓動PhoneCallListener您電話之前,傳你要打電話到PhoneCallListener,然後啓動callIntent數量。

在您的PhoneListener中,您可以檢查號碼是否與您在活動中傳遞的號碼相符。如果屬實,重新開始你的活動,否則什麼都不做。

編輯:

public void imgbtnCallPhone_Click(View view) { 
    EditText txtBusinessPhone = (EditText) findViewById(R.id.txtPhone); 

    // Get your PhoneCallListener and pass the number 
    PhoneCallListener mPhoneListener = new PhoneCallListener(); 
    mPhoneListener.yourActivity = true; 

    // start listening 
    TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); 
    tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); 

    try 
    { 
     final Intent callIntent = new Intent(android.content.Intent.ACTION_CALL); 
     callIntent.setData(Uri.parse("tel:"+ txtBusinessPhone.getText())); 
     startActivity(callIntent); 
    } catch (ActivityNotFoundException activityException) { 
     //Log.e("Calling a Phone Number", "Call failed", activityException); 
    } 

} 

你PhoneCallListener:

private class PhoneCallListener extends PhoneStateListener { 
    private boolean isPhoneCalling = false; 
    public Boolean yourActivity = false; 

    String LOG_TAG = "LOGGING 123"; 

    @Override 
    public void onCallStateChanged(int state, String incomingNumber) { 

     if (TelephonyManager.CALL_STATE_RINGING == state) { 
      // phone ringing 
      //Log.i(LOG_TAG, "RINGING, number: " + incomingNumber); 
     } 

     if (TelephonyManager.CALL_STATE_OFFHOOK == state) { 
      // active 
      //Log.i(LOG_TAG, "OFFHOOK"); 

      isPhoneCalling = true; 
     } 

     if (TelephonyManager.CALL_STATE_IDLE == state) { 
      // run when class initial and phone call ended, 
      // need detect flag from CALL_STATE_OFFHOOK 
      //Log.i(LOG_TAG, "IDLE"); 

      if (isPhoneCalling && yourActivity) { 

       //Log.i(LOG_TAG, "restart app"); 
       // restart app 
       Intent i = getBaseContext().getPackageManager() 
        .getLaunchIntentForPackage(
         getBaseContext().getPackageName()); 
       i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(i); 

       yourActivity = false; 
       isPhoneCalling = false; 
      } 
     } 
    } 
} 

我沒有測試它,因此它可能包含了一些錯誤。

+0

謝謝,請提供示例,因爲我是新的android –

+0

我編輯我的答案 – Manu

+0

此代碼中的問題是,我無法找到TelephonyManager.CALL_STATE_IDLE狀態中的incomingnumber。那個州給了我空白的號碼。 –

相關問題