Track a phone call duration

后端 未结 2 1557
余生分开走
余生分开走 2020-12-03 04:24

Is it possible to utilize the users phone through their cell provider, and track the length of a phone call?

So the user presses a button in the app \"Call Now\". A

2条回答
  •  孤街浪徒
    2020-12-03 04:29

    To calculate time talked for both incoming , outgoing calls use the following broadcast receiver :

    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
                }
            }
        }
    

    Register your receiver in your manifest file like this:

     
           
               
           
        
    

    Also add the uses permission:

    
    

提交回复
热议问题