2011-07-07 25 views
5

我使用這個:如何找到電話經理呼出號碼

public void onCallStateChanged(int state, String incomingNumber) 

這是聽:

telephonyManager.listen(listener,PhoneStateListener.LISTEN_CALL_STATE); 

我想知道這兩個呼出和呼入電話,但現在我只得到來電(當狀態改變振鈴時)。任何人都可以告訴我什麼時候可以檢測到傳出呼叫,並且它的結束

也有一種方法來模擬Eclipse模擬器中的傳出調用。能夠通過eclipse中的模擬器控制爲來話呼叫做到這一點。

回答

13

使用具有意圖的廣播偵聽器android.intent.action.NEW_OUTGOING_CALL字符串參數爲IntentFilter,並且不要忘記在AndroidMenifest中授予PROCESS_OUTGOING_CALLS的權限。這將工作。每當出現呼叫時,都會顯示Toast消息。代碼如下。

public static final String outgoing = "android.intent.action.NEW_OUTGOING_CALL" ; 
IntentFilter intentFilter = new IntentFilter(outgoing); 
BroadcastReceiver OutGoingCallReceiver = new BroadcastReceiver() 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     // TODO Auto-generated method stub 
     String outgoingno = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
     Toast.makeText(context, "outgoingnum =" + outgoingno,Toast.LENGTH_LONG).show(); 
    } 
}; 
registerReceiver(brForOutgoingCall, intentFilter); 
1

創建一個新的類,讓說MyPhoneReceiver,從廣播接收器繼承它,並實現的onReceive方法。

public class MyPhoneReceiver extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent){ 

     String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); 

    } 
} 

在另一類,讓說,MainActivity.class onCreate方法內。例如。

IntentFilter filter = new IntentFilter("android.intent.action.NEW_OUTGOING_CALL"); 
    MyPhoneReceiver myPhoneReceiver = new MyPhoneReceiver(); 
    registerReceiver(myPhoneReceiver,filter); 

AndroidManifest.xml中

<receiver 
    android:name=".MyPhoneReceiver"> 
    <intent-filter> 
    <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
    </intent-filter> 
</receiver> 

,並在AndroidManifest.xml中添加:

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