跟踪电话通话时间

spe*_*tak 8 android phone-call broadcastreceiver android-intent

是否可以通过其小区提供商使用用户电话,并跟踪电话的长度?

因此,用户按下应用程序"立即呼叫"中的按钮.呼叫开始于预先确定的号码.我们记录开始时间.当通话结束时,我们计算使用了多少分钟.

可能?

Akh*_*hil 10

要计算传入和传出呼叫的通话时间,请使用以下广播接收器:

public class CallDurationReceiver extends BroadcastReceiver {

    static boolean flag = false;
    static long start_time, end_time;

    @Override
    public void onReceive(Context arg0, Intent intent) {
        String action = intent.getAction();
        if (action.equalsIgnoreCase("android.intent.action.PHONE_STATE")) {
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
            TelephonyManager.EXTRA_STATE_RINGING)) {
                start_time = System.currentTimeMillis();
            }
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
            TelephonyManager.EXTRA_STATE_IDLE)) {
                end_time = System.currentTimeMillis();
                //Total time talked =
                long total_time = end_time - start_time;
                //Store total_time somewhere or pass it to an Activity using intent
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

在清单文件中注册您的接收器,如下所示:

 <receiver android:name=".CallDurationReceiver">
       <intent-filter>
           <action android:name="android.intent.action.PHONE_STATE" />
       </intent-filter>
    </receiver>
Run Code Online (Sandbox Code Playgroud)

还要添加使用权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Run Code Online (Sandbox Code Playgroud)

  • 但是,计算这样的持续时间将包括拾取时间,导致估计超过实际持续时间.我也正在寻找解决方案,似乎呼叫日志是唯一可以找到精确持续时间信息的地方. (10认同)