0

I am calling from my app. After ends that call, I am trying to get a call duration. But my code return call duration two times. Last call duration while making call and new call duration after ends a call. But I need only one call duration after user ends the new call. How can i achieve this. Here is my code. Thanks in advance.

public class MainActivity extends AppCompatActivity {

private static final int CALL_PERMISSION= 99;
private static final int READ_LOG_PERMISSION=11;
Button btnGetCallDuration;
Context mContext;
private int mFlag = 0;
String phNumber = "";
boolean flag=false;
String callDuration = "";
public static int REQ_CODE=1;
private boolean callPermission=false;
private boolean readlogPermission=false;
public static final int MULTIPLE_PERMISSIONS = 10; // code you want.

String[] permissions = new String[] {
        Manifest.permission.READ_CALL_LOG,
        Manifest.permission.CALL_PHONE,
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mContext = MainActivity.this;

    btnGetCallDuration = (Button) findViewById(R.id.btnGetCallDuration);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
       checkPermissions();
    }

    btnGetCallDuration.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

                    Log.v("onclick", "called");
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:121"));
                    startActivity(intent);
                    //flag = true;
        }
    });
}

private boolean checkPermissions() {
    int result;
    List<String> listPermissionsNeeded = new ArrayList<>();
    for (String p:permissions) {
        result = ContextCompat.checkSelfPermission(mContext,p);
        if (result != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(p);
        }
    }
    if (!listPermissionsNeeded.isEmpty()) {
        ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new
                String[listPermissionsNeeded.size()]), MULTIPLE_PERMISSIONS);
        return false;
    }

    return true;
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Log.v("request code",requestCode+"\t"+resultCode);

    if(requestCode==REQ_CODE)
    {
        Log.v("request code",requestCode+"");
       // getCallDruation();
    }

}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MULTIPLE_PERMISSIONS: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permissions granted.
                callPermission=true;
            } else {
                // no permissions granted.
                callPermission=false;
            }
            return;
        }
    }
}


@Override
protected void onPause() {
    super.onPause();
    Log.v("Pause is called","pause");
    flag=true;
}

public boolean checkLocationPermission() {
    if (ContextCompat.checkSelfPermission(mContext,
            Manifest.permission.CALL_PHONE)
            != PackageManager.PERMISSION_GRANTED) {

        // Asking user if explanation is needed
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                Manifest.permission.CALL_PHONE)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

            //Prompt the user once explanation has been shown
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.CALL_PHONE},
                    CALL_PERMISSION);


        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.CALL_PHONE},
                    CALL_PERMISSION);
        }
        return false;
    } else {
        return true;
    }
}

public boolean readCallLogPermission() {
    if (ContextCompat.checkSelfPermission(mContext,
            Manifest.permission.READ_CALL_LOG)
            != PackageManager.PERMISSION_GRANTED) {

        // Asking user if explanation is needed
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                Manifest.permission.READ_CALL_LOG)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

            //Prompt the user once explanation has been shown
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.READ_CALL_LOG},
                    READ_LOG_PERMISSION);


        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.CALL_PHONE},
                    READ_LOG_PERMISSION);
        }
        return false;
    } else {
        callPermission=true;
        return callPermission;
    }
}


@Override
protected void onResume() {
    super.onResume();
    Log.v("flag","onrsume called");

    if(flag)
    {
        getCallDruation();
        flag=false;
    }

}

private void getCallDruation() {
    StringBuffer sb = new StringBuffer();
    Uri contacts = CallLog.Calls.CONTENT_URI;

    Log.v("get duration","called");
    Cursor cur = getContentResolver().query(contacts, null, null, null, android.provider.CallLog.Calls.DATE + " DESC");

    int number = cur.getColumnIndex(CallLog.Calls.NUMBER);
    int duration = cur.getColumnIndex(CallLog.Calls.DURATION);
    sb.append("Call Details : \n");
    while (cur.moveToNext()) {
        phNumber = cur.getString(number);
        callDuration = cur.getString(duration);
        sb.append("\nPhone Number:" + phNumber+"\t"+callDuration);
        break;
    }
    cur.close();
    String str = sb.toString();
    Log.v("restult",str);
    Toast.makeText(mContext, phNumber + "\t\t" + callDuration, Toast.LENGTH_SHORT).show();

}

 }

This above code is called from onResume() life cycle method of activity.

Janaki
  • 145
  • 2
  • 16
  • 1
    Possible duplicate of [Track a phone call duration](https://stackoverflow.com/questions/10032542/track-a-phone-call-duration) – Piotr Golinski Jul 31 '17 at 11:01
  • @PiotrGolinski, provided link seems like, the duration is called manually. I need duration from call log. How can i get it. I have updated full code in my question. – Janaki Jul 31 '17 at 12:06
  • your title is confusing, `ongoing calls` --- `after user ends a call` which state do you want? , as ongoing call is not ongoing anymore if the user ends the call – Yazan Jul 31 '17 at 12:22
  • @Yazan, I am calling from my app. After ends that call, i need call duration of that call. This is what i need. – Janaki Jul 31 '17 at 12:29
  • 1
    you need a compination of BroadcastReceiver to listen to call-end event, and then query Logs for last call, have a look here https://stackoverflow.com/questions/42436808/get-last-call-details-from-call-log and here https://stackoverflow.com/questions/7237655/android-querying-call-log-after-it-has-been-updated-once-a-call-ends?lq=1 – Yazan Jul 31 '17 at 12:53
  • @Yazan, provided links are very useful and i got expected output. Thank you very much Yazan. – Janaki Aug 01 '17 at 05:00

0 Answers0