2011-12-29 37 views
-4
public void onReceive(Context context, Intent intent) { 
context.startService(new Intent(context,MyPhoneStateListener.class));  
MyPhoneStateListener lst=new MyPhoneStateListener(context); 
TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
telephonyManager.listen(lst, PhoneStateListener.LISTEN_CALL_STATE); 
} 

public IBinder onBind(Intent arg0) 
{ // TODO Auto-generated method stub return null; 

} 
} 

    // class MyPhoneStateListener extends PhoneStateListener 
{ 
    //private static final String TAG = "PhoneStateChanged"; 
    Context context; 
    //Context to make Toast if required 
    public MyPhoneStateListener(Context context) 
    { 
    super(); 
    this.context = context; 
    } 

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

switch (state) 
    { 
    case TelephonyManager.CALL_STATE_IDLE: //when Idle i.e no call 
    Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show(); 
    break; 
    case TelephonyManager.CALL_STATE_OFFHOOK: //when Off hook i.e in call //Make intent and start your service here Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show(); 
    break; 
    case TelephonyManager.CALL_STATE_RINGING: //when Ringing Toast.makeText(context, "Phone state Ringing", Toast.LENGTH_LONG).show(); 
    break; 
    default: break; 
} } } 

想要開發一個應用程序,在用戶呼叫時結束呼叫,首先應該從數據庫中檢測該號碼是否存儲。這將是更好的主意。要使用服務或廣播receiver.to結束通話,請幫助我已經嘗試了最後一個星期的代碼。我使用廣播接收器,但沒有幫助,因爲當電話合併時,除接收器之外的其他活動都會發生。有人撥打電話時結束通話

+1

誰會來編輯您的問題我親愛的朋友。 – 2011-12-29 12:45:46

+0

當您撥打的電話號碼不在您的列表中時,您是否想要撥打外線電話? – kingston 2011-12-29 13:45:46

回答

2

我假設你要刪除的情況下,撥出電話的被叫號碼不在名單:

您需要在您的清單註冊一個接收器:

<receiver 
     android:exported="true" 
     android:name=".receiver.OutgoingCall" > 
     <intent-filter android:priority="0" > 
      <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </receiver> 

然後按照this