2012-02-23 61 views
5

statrtActivity(callIntent),通話結束,然後我必須等待幾秒鐘,然後自動結束。最終我的電話我採取了mycalss擴展Broadcastreceiver然後在那onreceive()我執行。在那個方法我只能設置舊號碼和新號碼和吐司打印。 我想要的就是結束呼叫我需要寫的東西。以及如何從我的課程調用接受方法? plase幫助我。我沒有得到任何地方。如何結束我的傳出呼叫。

@Override 
       public void onReceive(Context context, Intent intent) {         
        final String oldNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);   
        this.setResultData(newPhNnumber);             
        final String newNumber = this.getResultData(); 
        if((newNumber!=null)&&(newNumber!=oldNumber)) 
        { 
        String msg = "Intercepted outgoing call. Old number " + oldNumber + ", new number " + newNumber; 
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show(); 
        this.abortBroadcast();----> what it does? 
       } 

回答

3

你可以試試這個:

TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
try { 
    Class c = Class.forName(tm.getClass().getName()); 
    Method m = c.getDeclaredMethod("getITelephony"); 
    m.setAccessible(true); 
    ITelephony telephonyService = (ITelephony) m.invoke(tm); 

    telephonyService.endCall(); 

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

我想你的代碼中止呼出,但似乎並沒有結束通話。我使用'TelephonyManager.CALL_STATE_OFFHOOK'來檢查狀態,然後結束它。任何想法? – 2015-01-14 05:50:53

0

您只需通過下面的代碼

public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 

    if (intent.getAction() 
      .equals("android.intent.action.NEW_OUTGOING_CALL")) { 

     phoneNo = intent 
       .getStringExtra(Intent.EXTRA_PHONE_NUMBER); 

     if (phoneNo.isBlocked(blockNo)) { 
      setResultData(null); 

     } 
    } 
} 
+0

'phoneNo'爲空...... – 2015-01-14 06:11:17

+0

如果你沒有添加它,加入這個「PROCESS_OUTGOING_CALLS」權限? – 2015-01-16 06:11:02

+0

是的,我弄清楚..我用電話狀態改變廣播而不是傳出..謝謝btw .. – 2015-01-17 08:19:08