2013-11-21 28 views
1

我正在開發中,我想保持跟蹤的呼出和duration.For例如,如果一個手機用戶撥打我用Intent.ACTION_NEW_OUTGOING_CALL呼叫的應用程序。* 當他切斷電話時,要執行什麼操作? *我參考了所有教程,他們建議使用設備調用日誌URI.But我想在broadcastreceiver.Thanks提前做。事件的Android

public class MyCallReceiver extends BroadcastReceiver{ 
     public void onReceive(Context context, Intent intent) { 

    if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) { 

     String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 

     }  

    } 
    } 
+0

請張貼了'ADB logcat'輸出,包括_caused by_的異常跟蹤的部分。 – 18446744073709551615

+0

@ 18446744073709551615當我運行這個應用程序時,我可以得到撥號的號碼。但我沒有例外。我要求動作撥號(ACTION_NEW_OUTGOING_CALL)任何其他結束呼叫的動作... – Ram

回答

1

試試這個:

Boolean wasnumberdialed=false; 
Boolean callpicked=false; 

//if dialed below is called 
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) { 
     String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
     wasnumberdialed=true; 
} 

//if the call is received by the recipient then this is executed and its an 
//outgoing call,(wasnumberdialed is set to true only for outgoing calls) we 
//need to ignore incoming calls. If wedont check this an incoming call 
//would also execute this. 
if (intent.getAction().equals(Intent.CALL_STATE_OFFHOOK && wasnumberdialed)) { 
     //start your timer to count the time of call 
     //long start_time = System.currentTimeMillis(); 
     callpicked=true; 
} 

//after the call is disconnected and was received which is an outgoing call below is 
//executed 
if (intent.getAction().equals(Intent.EXTRA_STATE_IDLE && callpicked)) { 
     //end your timer here 
     //long end_time = System.currentTimeMillis(); 
} 

Log.v("myapp","Total call time is "+TimeUnit.MILLISECONDS.toMinutes(end_time-start_time)+" mins);