2011-04-27 57 views
6

我們想攔截廣播接收者的呼出掛斷狀態。我們正在收聽android.intent.action.PHONE_STATE,並通知IDLE狀態,即通話結束時。在掛斷時攔截呼出

不幸的是,我們沒有從通話記錄內容提供商處獲得被叫號碼。它總是返回最後的呼叫。有趣的是,來電確實在意圖上發送了一個號碼,但沒有任何傳出呼叫。

如果我們使用android.intent.action.NEW_OUTGOING_CALL,電話號碼正在通過呼叫開始時的意圖,但由於我們希望等待呼叫完成,因此這個階段對於我們做任何處理都爲時尚早。

public class InterceptOutgoingCall extends BroadcastReceiver { 
Boolean isOutGoingCall = true; 
private static final String LOG_TAG = "InterceptOutgoingCall"; 

@Override 
public void onReceive(Context context, Intent intent) { 

    //1. Logging the intent params 

    String state = null; 
    StringBuffer buf = new StringBuffer(); 
    if (intent.getAction() != null) 
     buf.append("Intent action: " + intent.getAction()); 
    if (intent.getCategories() != null) { 
     Set<String> categories = intent.getCategories(); 
     if (categories != null) { 
      Iterator<String> it = categories.iterator(); 
      buf.append("; categories: "); 
      int ctr = 0; 
      for (; it.hasNext();) { 
       String category = (String) it.next(); 
       if (ctr != 0) 
        buf.append("/"); 
       buf.append(category); 
       ++ctr; 
      } 
     } 
    } 
    if (intent.getData() != null) { 
     buf.append("; intent data: " + intent.getData().toString()); 
    } 
    Bundle extras = intent.getExtras(); 
    if (extras != null) { 
     buf.append("; extras: "); 
     int ctr = 0; 

     Set keys = extras.keySet(); 
     for (Iterator it = keys.iterator(); it.hasNext();) { 
      String key = (String) it.next(); 
      Object value = extras.get(key); 
      if (ctr != 0) 
       buf.append("/"); 
      String strvalue = value == null ? "null" : value.toString(); 
      if (key.equals("state")) 
       state = strvalue; 
      buf.append(key + "=" + strvalue); 
      ++ctr; 
     } 
     Log.i(LOG_TAG, buf.toString()); 
     if ("IDLE".equals(state)) { 
      Log.i(LOG_TAG, "Number of the other party: " 
        + getLastCallLogEntry(context)); 
     } 
    } 

     String outgoingCall = CallLog.Calls.getLastOutgoingCall(context); 
     Log.i(LOG_TAG, "Last call:" + outgoingCall); 


} 

private String getLastCallLogEntry(Context context) { 
    String[] projection = new String[] { BaseColumns._ID, 
      CallLog.Calls.NUMBER, CallLog.Calls.TYPE }; 
    ContentResolver resolver = context.getContentResolver(); 
    Cursor cur = resolver.query(CallLog.Calls.CONTENT_URI, projection, 
      null, null, CallLog.Calls.DEFAULT_SORT_ORDER); 
    int numberColumn = cur.getColumnIndex(CallLog.Calls.NUMBER); 
    int typeColumn = cur.getColumnIndex(CallLog.Calls.TYPE); 
    if (!cur.moveToNext()) { 
     cur.close(); 
     return ""; 
    } 
    String number = cur.getString(numberColumn); 
    String type = cur.getString(typeColumn); 
    String dir = null; 
    try { 
     int dircode = Integer.parseInt(type); 
     switch (dircode) { 
     case CallLog.Calls.OUTGOING_TYPE: 
      dir = "OUTGOING"; 
      break; 

     case CallLog.Calls.INCOMING_TYPE: 
      dir = "INCOMING"; 
      break; 

     case CallLog.Calls.MISSED_TYPE: 
      dir = "MISSED"; 
      break; 
     } 
    } catch (NumberFormatException ex) { 
    } 
    if (dir == null) 
     dir = "Unknown, code: " + type; 
    cur.close(); 
    return dir + "," + number; 
} 

登錄貓

*當呼叫開始,NEW_OUTGOING_CALL廣播*

04-27 13:07:16.756: INFO/InterceptOutgoingCall(775): Intent action: android.intent.action.NEW_OUTGOING_CALL; extras: android.phone.extra.ALREADY_CALLED=false/android.intent.extra.PHONE_NUMBER=999222/android.phone.extra.ORIGINAL_URI=tel:999-222 

結果數據

04-27 13:07:16.876: INFO/InterceptOutgoingCall(775): Result Data:999222 

通話記錄最後一次通話

04-27 13:07:17.156: INFO/InterceptOutgoingCall(775): Last call:809090 

*接下來,PHONE_STATE是廣播,在演員沒有號碼*

04-27 13:07:19.495: INFO/InterceptOutgoingCall(775): Intent action: android.intent.action.PHONE_STATE; extras: state=OFFHOOK 

沒有結果數據

04-27 13:07:19.636: INFO/InterceptOutgoingCall(775): No result data 

當通話結束後,在演員無號

04-27 13:08:09.306: INFO/InterceptOutgoingCall(775): Intent action: android.intent.action.PHONE_STATE; extras: state=IDLE 

通話記錄中最後一項之前被叫號碼

04-27 13:08:09.627: INFO/InterceptOutgoingCall(775): Number of the other party: OUTGOING,809090 
04-27 13:08:09.675: INFO/InterceptOutgoingCall(775): No result data 
04-27 13:08:10.336: INFO/InterceptOutgoingCall(775): Last call:809090 

回答

0

您可以像下面的呼出號碼:

串號= intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

我想你可以使用公共靜態變量存儲數字,然後引用它。

2

針對IntentFilter使用具有intent android.intent.action.NEW_OUTGOING_CALL字符串參數的廣播偵聽器,並且不要忘記將AndroidMenifest的權限授予PROCESS_OUTGOING_CALLS。這將工作。

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);