android中调用结束进程的动作事件

时间:2013-11-21 09:30:31

标签: android android-intent broadcastreceiver phone-call

我正在开发一个应用程序,其中我想要跟踪拨出电话及其持续时间。例如,如果移动电话用户拨打电话我使用了Intent.ACTION_NEW_OUTGOING_CALL。* 当他切断电话时,要实现什么动作?。 *我提到了所有教程,他们建议使用设备调用日志URI。但我想在broadcastreceiver.Thanks提前做。

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

       }    

    }
  }

1 个答案:

答案 0 :(得分: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);