2017-01-12 24 views
1

我正在開發一款應用程序,該應用程序僅用於測試目的。我搜索了很多,並嘗試不同的建議,如不同的帖子中建議,但似乎沒有爲我工作。Android如何檢測傳出呼叫是否應答

我接受任何建議,如思考,輔助服務,根或任何其他黑客。請幫忙。

問候

回答

1

試試這個

坐落在manifest.xml文件所有需要的權限。

調用這個類中的服務

public class PhoneListener extends PhoneStateListener { 

private static PhoneListener instance = null; 

/** 
* Must be called once on app startup 
* 
* @param context - application context 
* @return 
*/ 
public static PhoneListener getInstance(Context context) { 
    if (instance == null) { 
     instance = new PhoneListener(context); 
    } 
    return instance; 
} 

public static boolean hasInstance() { 
    return null != instance; 
} 

private final Context context; 
private CallLog phoneCall; 

private PhoneListener(Context context) { 
    this.context = context; 
} 

AtomicBoolean isRecording = new AtomicBoolean(); 
AtomicBoolean isWhitelisted = new AtomicBoolean(); 


/** 
* Set the outgoing phone number 
* <p/> 
* Called by {@link MyCallReceiver} since that is where the phone number is available in a outgoing call 
* 
* @param phoneNumber 
*/ 
public void setOutgoing(String phoneNumber) { 
    if (null == phoneCall) 
     phoneCall = new CallLog(); 
    phoneCall.setPhoneNumber(phoneNumber); 
    phoneCall.setOutgoing(); 
    // called here so as not to miss recording part of the conversation in TelephonyManager.CALL_STATE_OFFHOOK 
    isWhitelisted.set(Database.isWhitelisted(context, phoneCall.getPhoneNumber())); 
} 

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

    switch (state) { 
     case TelephonyManager.CALL_STATE_IDLE: // Idle... no call 
      if (isRecording.get()) { 
       RecordCallService.stopRecording(context); 
       phoneCall = null; 
       isRecording.set(false); 
      } 
      break; 
     case TelephonyManager.CALL_STATE_OFFHOOK: // Call answered 
      if (isWhitelisted.get()) { 
       isWhitelisted.set(false); 
       return; 
      } 
      if (!isRecording.get()) { 
       isRecording.set(true); 
       // start: Probably not ever usefull 
       if (null == phoneCall) 
        phoneCall = new CallLog(); 
       if (!incomingNumber.isEmpty()) { 
        phoneCall.setPhoneNumber(incomingNumber); 
       } 
       // end: Probably not ever usefull 
       RecordCallService.sartRecording(context, phoneCall); 
      } 
      break; 
     case TelephonyManager.CALL_STATE_RINGING: // Phone ringing 
      // DO NOT try RECORDING here! Leads to VERY poor quality recordings 
      // I think something is not fully settled with the Incoming phone call when we get CALL_STATE_RINGING 
      // a "SystemClock.sleep(1000);" in the code will allow the incoming call to stabilize and produce a good recording...(as proof of above) 
      if (null == phoneCall) 
       phoneCall = new CallLog(); 
      if (!incomingNumber.isEmpty()) { 
       phoneCall.setPhoneNumber(incomingNumber); 
       // called here so as not to miss recording part of the conversation in TelephonyManager.CALL_STATE_OFFHOOK 
       isWhitelisted.set(Database.isWhitelisted(context, phoneCall.getPhoneNumber())); 
      } 
      break; 
    } 

} 

}

並使用廣播接收器

public class MyCallReceiver extends BroadcastReceiver { 

public MyCallReceiver() { 
} 

static TelephonyManager manager; 

@Override 
public void onReceive(Context context, Intent intent) { 
    Log.i("JLCreativeCallRecorder", "MyCallReceiver.onReceive "); 

    if (!AppPreferences.getInstance(context).isRecordingEnabled()) { 
     removeListener(); 
     return; 
    } 

    if (Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) { 
     if (!AppPreferences.getInstance(context).isRecordingOutgoingEnabled()) { 
      removeListener(); 
      return; 
     } 
     PhoneListener.getInstance(context).setOutgoing(intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER)); 
    } else { 
     if (!AppPreferences.getInstance(context).isRecordingIncomingEnabled()) { 
      removeListener(); 
      return; 
     } 
    } 

    // Start Listening to the call.... 
    if (null == manager) { 
     manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
    } 
    if (null != manager) 
     manager.listen(PhoneListener.getInstance(context), PhoneStateListener.LISTEN_CALL_STATE); 
} 

private void removeListener() { 
    if (null != manager) { 
     if (PhoneListener.hasInstance()) 
      manager.listen(PhoneListener.getInstance(null), PhoneStateListener.LISTEN_NONE); 
    } 
} 

}

我希望你得到這個代碼一些幫助。

感謝

+0

感謝您試圖提供幫助,但上述代碼無法分辨何時**完全**電話已被接聽! –

+0

此問題可能是由於您的設備的雙卡或缺少phoneListener。在三星設備的工作成功。 –

0

您需要在清單

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

TelephonyManager此權限有一個聽衆獲得手機狀態。實施這個知道如果電話鈴聲或通話。

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
     PhoneStateListener callStateListener = new PhoneStateListener() { 

      public void onCallStateChanged(int state, String incomingNumber) { 
       // TODO React to a incoming call. 

       try { 
        if (state == TelephonyManager.CALL_STATE_RINGING) { 
         Toast.makeText(getApplicationContext(), "Phone Is Ringing" + incomingNumber, Toast.LENGTH_LONG).show(); 
         number = incomingNumber; 
         //g.setPhoneNo(incomingNumber); 
         AndroidNetCommunicationClientActivity.mMsgSendRequest("CommandMsgCallIncoming" + number); 
        } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) { 
         Toast.makeText(getApplicationContext(), "Phone is Currently in A call" + incomingNumber, Toast.LENGTH_LONG).show(); 
         //number = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
         number = incomingNumber; 


        } else if (state == TelephonyManager.DATA_DISCONNECTED) { 
         number = ""; 
         CallID = ""; 
        } 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        //conn.MessageBox(MainActivity.this, e.getMessage()); 
        e.printStackTrace(); 
       } 
       super.onCallStateChanged(state, incomingNumber); 

      } 
     }; 
     telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE); 
+0

CALL_STATE_OFFHOOK在撥打號碼後立即被呼叫! –

1

您可以使用可訪問性事件檢測通話時長和,如果呼出回答與否,你可以檢測..

我已經Here詳細回答了`你可以檢查一下。