2016-02-09 88 views
-2

如何檢測傳出呼叫連接。我的代碼如下,但有incomingcall若在該得到,如果(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING))如何檢測傳出呼叫連接android

時,沒有活動就在這個

得到否則如果(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE))

當incomingcall是回答它會得到這個和它的呼叫它會得到這個。

否則,如果(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK))

,現在當outgoingcall它的狀態是摘機我怎麼能知道我的outgoingcall連接時。

謝謝

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO: This method is called when the BroadcastReceiver is receiving 
    // an Intent broadcast. 
    if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)){ 
     String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); 
     Toast.makeText(context, "Call From : " + incomingNumber, Toast.LENGTH_LONG).show(); 

    } 
    else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE)){ 
     Toast.makeText(context,TelephonyManager.EXTRA_STATE_IDLE,Toast.LENGTH_LONG).show(); 
    } 
    else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){ 
     Toast.makeText(context,TelephonyManager.EXTRA_STATE_OFFHOOK,Toast.LENGTH_LONG).show(); 
    } 
} 
+0

http://stackoverflow.com/questions/12039098/android-detecting-when-lines-have-been-connected-during-an-outgoing-call請檢查下面的鏈接。 –

回答

0

在Android清單文件中添加這一點。聲明廣播接收器。

<receiver 
     android:name=".OutCallLogger" 
     android:enabled="true" 
     android:exported="true" > 
      <intent-filter> 
       <action android:name="android.intent.action.PRECISE_CALL_STATE" /> 
       <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
      </intent-filter> 
    </receiver> 

在Android清單文件中添加以下權限。

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

也在清單文件中添加此行。

<uses-feature android:name="android.hardware.telephony"> 
</uses-feature> 

這是您的班級,將用於獲取撥出呼叫的精確呼叫狀態。

public class OutCallLogger extends BroadcastReceiver { 
public OutCallLogger() { 
} 
TelephonyManager Tm; 
ITelephony telephonyService; 
Class c = null; 
Method methodGetInstance = null; 
Method methodGetActiveFgCallState=null; 
String TAG="Tag"; 
Object objectCallManager=null; 
Context context1; 
Class<?> classCallManager; 

Class telephonyClass; 
Class telephonyStubClass; 
Class serviceManagerClass; 
Class serviceManagerStubClass; 
Class serviceManagerNativeClass; 
Class serviceManagerNativeStubClass; 

Method telephonyCall; 
Method telephonyEndCall; 
Method telephonyAnswerCall; 
Method getDefault; 

Method[] temps; 
Constructor[] serviceManagerConstructor; 

// Method getService; 
Object telephonyObject; 
Object serviceManagerObject; 
private Timer timer= null; 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO: This method is called when the BroadcastReceiver is receiving 
    // an Intent broadcast. 



    this.context1= context; 
    try { 
     //String serviceManagerName = "android.os.IServiceManager"; 
     String serviceManagerName = "android.os.ServiceManager"; 
     String serviceManagerNativeName = "android.os.ServiceManagerNative"; 
     String telephonyName = "com.android.internal.telephony.ITelephony"; 


     telephonyClass = Class.forName(telephonyName); 
     telephonyStubClass = telephonyClass.getClasses()[0]; 
     serviceManagerClass = Class.forName(serviceManagerName); 
     serviceManagerNativeClass = Class.forName(serviceManagerNativeName); 

     Method getService = // getDefaults[29]; 
       serviceManagerClass.getMethod("getService", String.class); 

     Method tempInterfaceMethod = serviceManagerNativeClass.getMethod(
       "asInterface", IBinder.class); 

     Binder tmpBinder = new Binder(); 
     tmpBinder.attachInterface(null, "fake"); 

     serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder); 
     IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone"); 
     Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class); 
     Method[] aClassMethods = telephonyClass.getDeclaredMethods(); 
     for(Method m : aClassMethods) 
     { 
      //Log.e("MEthods", m.getName()); 
     } 
     telephonyObject = serviceMethod.invoke(null, retbinder); 
     //telephonyCall = telephonyClass.getMethod("call", String.class); 
     telephonyEndCall = telephonyClass.getMethod("getPreciseCallState"); 
     //telephonyAnswerCall = telephonyClass.getMethod("answerRingingCall"); 
     Log.e("CallState",telephonyEndCall.invoke(telephonyObject).toString()); 
     Toast.makeText(context1, " "+telephonyEndCall.invoke(telephonyObject).toString(), Toast.LENGTH_LONG).show(); 

     telephonyEndCall.invoke(telephonyObject); 

    } catch (Exception e) { 
     e.printStackTrace(); 

    } 

} 

}

+0

吐司會出現,它會告訴你精確的呼叫狀態。您可以相應地修改您的代碼。上面的代碼只能在來電時使用。 –

+0

謝謝。我已經嘗試過你的代碼,但我什麼也沒得到。當客戶回答時不要敬酒。我不知道我做錯了什麼。 @Hammad Tariq Sahi – bigcenima

+0

當您打出電話時,您是否得到烤麪包? –