2017-08-24 48 views
-1

我想知道傳出的呼叫是否在android程序中被回答。我只是想知道傳出的呼叫是否已經回答。我是新的android.Please幫我解決這個問題。爲了獲得通話的回答時間我用這個。如何知道傳出的電話已回答或不?

我想知道傳出的電話是否在android程序中回答。我只想知道傳出的電話是否已經回答。我是Android新手。請幫我解決這個問題。爲了接聽電話,我使用了這個功能。

BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) { 
      savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER"); 


     } else { 
      String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE); 
      String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
      int state = 0; 
      if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) { 
       state = TelephonyManager.CALL_STATE_IDLE; 
      } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { 
       state = TelephonyManager.CALL_STATE_OFFHOOK; 
      } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
       state = TelephonyManager.CALL_STATE_RINGING; 
      } 

      onCallStateChanged(context, state, number); 
     } 
    } 
}; 


protected void onIncomingCallStarted(Context ctx, String number, Date start) { 
} 

protected void onOutgoingCallStarted(Context ctx, String number, Date start) { 


} 

protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end) { 
} 

protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end) { 

    SimpleDateFormat localDateFormat = new SimpleDateFormat("HH:mm:ss"); 
    startime = localDateFormat.format(start); 
    endtime = localDateFormat.format(end); 

    long aa = start.getTime(); 
    long bb = end.getTime(); 
    long cc = bb - aa; 
    long diffMinutes = cc/1000 % 60; 
    duration = Long.toString(diffMinutes); 

    Bundle bundle = new Bundle(); 
    bundle.putString("start", startime); 
    bundle.putString("end", endtime); 
    bundle.putString("duration", duration); 

    Call_End dFragment = new Call_End(); 
    dFragment.setArguments(bundle); 
    dFragment.show(fragmentManager, "Dialog Fragment"); 

} 

protected void onMissedCall(Context ctx, String number, Date start) { 
} 

public void onCallStateChanged(Context context, int state, String number) { 
    if (lastState == state) { 
     //No change, debounce extras 
     return; 
    } 
    switch (state) { 


     case TelephonyManager.CALL_STATE_RINGING: 
      isIncoming = true; 
      callStartTime = new Date(); 
      savedNumber = number; 
      onIncomingCallStarted(context, number, callStartTime); 
      break; 
     case TelephonyManager.CALL_STATE_OFFHOOK: 

      callStartTime = new Date(); 
       onOutgoingCallStarted(context, savedNumber, callStartTime); 

      //Transition of ringing->offhook are pickups of incoming calls. Nothing done on them 
      if (lastState != TelephonyManager.CALL_STATE_RINGING) { 
       isIncoming = false; 
       callStartTime = new Date(); 
       onOutgoingCallStarted(context, savedNumber, callStartTime); 
    } 
      break; 
     case TelephonyManager.CALL_STATE_IDLE: 
      //Went to idle- this is the end of a call. What type depends on previous state(s) 
      if (lastState == TelephonyManager.CALL_STATE_RINGING) { 
       //Ring but no pickup- a miss 
       onMissedCall(context, savedNumber, callStartTime); 
      } else if (isIncoming) { 
       onIncomingCallEnded(context, savedNumber, callStartTime, new Date()); 
      } else { 
       onOutgoingCallEnded(context, savedNumber, callStartTime, new Date()); 
      } 
      break; 
    } 
    lastState = state; 
} 

回答

0
@Override 
public void onReceive(Context context, Intent intent) { 
    Log.d("Intent", intent.toString()); 
    if(intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) 
    { 
     Log.e("Call_state","outgoing call detected"); 
    } 

//  Checking for the call status 
    try { 
     // TELEPHONY MANAGER class object to register one listner 
     TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
     //Create Listner 
     MyPhoneStateListener PhoneListener = new MyPhoneStateListener(); 
     // Register listener for LISTEN_CALL_STATE 
     tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private class MyPhoneStateListener extends PhoneStateListener { 
    public void onCallStateChanged(int state, String incomingNumber) { 
     switch (state) { 
      case TelephonyManager.CALL_STATE_RINGING: 
       Log.e("Call_state","call ringing"); 
       break; 
      case TelephonyManager.CALL_STATE_OFFHOOK: 
       Log.e("Call_state","call offhook"); 
       break; 
      case TelephonyManager.CALL_STATE_IDLE: 
       Log.e("Call_state","call idle"); 
       break; 
     } 
    } 
} 
0

你可以這樣做,但是這將不是所有手機上運行。

  1. 創建通知監聽器服務,告訴你另一個人接聽電話。 public class CallNotification extends NotificationListenerService { public void onNotificationPosted(StatusBarNotification sbn) { //your code here after you getting notification another person has pick up the call } }

  2. 詢問系統允許通知。

    Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); startActivity(intent);