Tracking phone call time in real time - java

I have a recording call app in which i would like to track the call duration in realtime , i have looked up some codes online but none of them does what i want , i'm here seeking help from those who are expert in this , any way to do that please , Thank you .
Basically when the call is answered , track call timing 0 1 2 3 and so on .
Tried this code but only gets the final duration
private void getCallDetails() {
StringBuffer sb = new StringBuffer();
Uri contacts = CallLog.Calls.CONTENT_URI;
Cursor managedCursor = this.getContentResolver().query(contacts, null, null, null, null);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("Call Details :");
while (managedCursor.moveToNext()) {
HashMap rowDataCall = new HashMap<String, String>();
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
String callDayTime = new Date(Long.valueOf(callDate)).toString();
// long timestamp = convertDateToTimestamp(callDayTime);
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
Log.d("TAG","Call Duration" + callDuration);
}
managedCursor.close();
System.out.println(sb);
}

Related

How to add call logs List in android java?

I want to add call logs in List
my code working fine but it's showing only one call log,
any solution for it,
I am beginner please check my code image and help me.
[1]: https://i.stack.imgur.com/joGU1.png
//Recycler Function
recyclerView_cl = view.findViewById(R.id.receyclerView_cl);
LinearLayoutManager LayoutManager = new LinearLayoutManager(view.getContext());
LayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView_cl.setLayoutManager(LayoutManager);
Cursor managedCursor = getActivity().managedQuery(CallLog.Calls.CONTENT_URI, null,
null, null, null);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
while (managedCursor.move(3)) {
List<CallModelClass> modelClassList = new ArrayList<>();
CallAdapter adapter = new CallAdapter(modelClassList);
modelClassList.add(new CallModelClass("name from", "call type", "sasa"));
recyclerView_cl.setAdapter(adapter);
adapter.notifyDataSetChanged();
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
modelClassList.add(new CallModelClass(phNumber,dir,callDate));
}
managedCursor.close();
Move these two lines out of your while loop(put theme above the loop call):
List<CallModelClass> modelClassList = new ArrayList<>();
CallAdapter adapter = new CallAdapter(modelClassList);
And also, call this: adapter.notifyDataSetChanged(); after your while loop end.

Date comparision not working in my application

the if statement in the function getCallDetailsAgil() is not working. the statement is in the switch case where it is checking if the call type is MISSED
Can anyone please help me out? startdate and enddate are publiic date objects. i am just trying to ocunt all missed calls between thos two dates.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_incoming);
Intent i = getIntent();
//
}
private int getCallDetailsAgil() {
StringBuffer sb = new StringBuffer();
Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, null);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int count=0;
while (managedCursor.moveToNext()) {
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
Date dc = new Date(Long.valueOf(callDate));
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.INCOMING_TYPE:
System.out.println("---------" + startdate.toString());
System.out.println(dc.toString());
System.out.println("---------" + enddate.toString());
System.out.println(dc.before(startdate)+" "+dc.before(enddate));
if (dc.before(startdate) && dc.before(enddate))
count++;
break;
}
}
return count;
}

Sending multiline string into listViewAdapter

I have the following method in my class:
private String getCallDetails()
{
StringBuilder sb = new StringBuilder();
Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI,
null, null, null,null);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
while (managedCursor.moveToNext()) {
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String callDirection = null;
int callDirectionCode = Integer.parseInt(callType);
switch (callDirectionCode)
{
case CallLog.Calls.OUTGOING_TYPE:
callDirection = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
callDirection = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
callDirection = "MISSED";
break;
}
sb.append(callDirection+"||"+phNumber+"||"+callDuration+"||"+callDayTime+";\n");
}
managedCursor.close();
return sb.toString();
}
which returns the following output:
2018-09-12 17:43:55.253 6883-6883/test.package.android D/callDetails: OUTGOING||17||9||Tue Aug 28 11:09:21 GMT+02:00 2018;
OUTGOING||17||0||Fri Aug 31 15:03:34 GMT+02:00 2018;
INCOMING||17||13||Fri Aug 31 15:17:00 GMT+02:00 2018;
What I want to do is separate each line and in each line separate items divided with "||" and set them as strings that I can use. How can I do this

How to get the Call Log data between two dates and Total number of incoming an outgoing calls between the dates

I want to get the call log history by today, yesterday, last seven days and last 30days along with that i want to show the total duration of incoming and outgoing calls of that particular number.
I tried with various type but none of them working. What should i do to get the output ?.I have no idea how to use the query to get the call log from specific date. Help me Thanks
public class CallLogActivity extends Activity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call_log);
textView = (TextView) findViewById(R.id.textCallBetween);
textView.setVisibility(View.GONE);
// listcallLog = (ListView) findViewById(R.id.calllogItems);
getCalldetails();
}
public void getCalldetails() {
StringBuffer stringBuffer = new StringBuffer();
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
Calendar calender = Calendar.getInstance();
calender.set(2016, calender.NOVEMBER, 18);
String fromDate = String.valueOf(calender.getTime());
calender.set(2016, Calendar.NOVEMBER, 20);
String toDate = String.valueOf(calender.getTimeInMillis());
String[] whereValue = {fromDate,toDate};
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, android.provider.CallLog.Calls.DATE + " BETWEEN ? AND ?", whereValue, strOrder);
// Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, android.provider.CallLog.Calls.DATE, new String[]{" BETWEEN ? AND ?"}, strOrder);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
stringBuffer.append("Call Log :");
while (managedCursor.moveToNext())
{
String phoneNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
SimpleDateFormat formatter = new SimpleDateFormat(
"dd-MMM-yyyy HH:mm");
String dateString = formatter.format(new Date(Long
.parseLong(callDate)));
// Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String dir = null;
int dirCode = Integer.parseInt(callType);
switch (dirCode)
{
case CallLog.Calls.OUTGOING_TYPE :
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED CALL";
break;
}
stringBuffer.append("\nPhone Number:--- " + phoneNumber + "\nCall Type:--- "
+ dir + "\nCall Date:---"
+ dateString + "\nCall Duration:---" + callDuration);
stringBuffer.append("\n--------------------------");
}
textView.setText(stringBuffer);
textView.setVisibility(View.VISIBLE);
}`
}
The below code is the solution for getting call log data between two dates.
public class CallLogActivity extends Activity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call_log);
textView = (TextView) findViewById(R.id.textCallBetween);
textView.setVisibility(View.GONE);
// listcallLog = (ListView) findViewById(R.id.calllogItems);
getCalldetails();
}
public void getCalldetails() {
StringBuffer stringBuffer = new StringBuffer();
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
Calendar calender = Calendar.getInstance();
calender.set(2016, calender.NOVEMBER, 18);
String fromDate = String.valueOf(calender.getTimeInMillis());
calender.set(2016, calender.NOVEMBER, 20);
String toDate = String.valueOf(calender.getTimeInMillis());
String[] whereValue = {fromDate,toDate};
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, android.provider.CallLog.Calls.DATE + " BETWEEN ? AND ?", whereValue, strOrder);
// Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, android.provider.CallLog.Calls.DATE, new String[]{" BETWEEN ? AND ?"}, strOrder);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
stringBuffer.append("Call Log :");
while (managedCursor.moveToNext())
{
String phoneNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
SimpleDateFormat formatter = new SimpleDateFormat(
"dd-MMM-yyyy HH:mm");
String dateString = formatter.format(new Date(Long
.parseLong(callDate)));
// Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String dir = null;
int dirCode = Integer.parseInt(callType);
switch (dirCode)
{
case CallLog.Calls.OUTGOING_TYPE :
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED CALL";
break;
}
stringBuffer.append("\nPhone Number:--- " + phoneNumber + "\nCall Type:--- "
+ dir + "\nCall Date:---"
+ dateString + "\nCall Duration:---" + callDuration);
stringBuffer.append("\n--------------------------");
}
textView.setText(stringBuffer);
textView.setVisibility(View.VISIBLE);
}
}
go step by step . first get all call log history this way.
Uri allCalls = Uri.parse("content://call_log/calls");
Cursor c = managedQuery(allCalls, null, null, null, null);
String num= c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));// for number
String name= c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));// for name
String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));// for duration
int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));// for call type, Incoming or out going.

getContentResolver().query with CallLog

I'm working on an Android app. I need to get call log to get info about outcoming calls. I´m using this one (from some topic here):
private void getCallDetails() {
StringBuffer sb = new StringBuffer();
Cursor managedCursor = managedQuery( CallLog.Calls.CONTENT_URI,null, null,null, null);
int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER );
int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
sb.append( "Call Details :");
while ( managedCursor.moveToNext() ) {
String phNumber = managedCursor.getString( number );
String callType = managedCursor.getString( type );
String callDate = managedCursor.getString( date );
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString( duration );
String dir = null;
int dircode = Integer.parseInt( callType );
switch( dircode ) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
sb.append( "\nPhone Number:--- "+phNumber +" \nCall Type:--- "+dir+" \nCall Date:--- "+callDayTime+" \nCall duration in sec :--- "+callDuration );
sb.append("\n----------------------------------");
}
managedCursor.close();
call.setText(sb);
}
I can use this newer code too:
Cursor c = getContentResolver().query(CallLog.Calls.CONTENT_URI,null, null,null, null);
but in both examples my app crashes on phone even on emulator. I tried to fix that only think I find out is that this query line is the problem but only with CallLog.
When I use similar code with sms for example everythink is working fine.
public StringBuffer getOutgoingSMSContent() {
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse("content://sms/sent/");
StringBuffer messagedata = new StringBuffer();
int count = 0;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor.getCount() != 0) {
if (cursor.moveToFirst()) {
do {
messagedata.append("Outgoing message count: " + (count + 1)
+ "\n");
for (int m = 0; m < cursor.getColumnCount(); m++) {
if (cursor.getColumnName(m).equalsIgnoreCase("address")
|| cursor.getColumnName(m).equalsIgnoreCase(
"date")
|| cursor.getColumnName(m).equalsIgnoreCase(
"body")
|| cursor.getColumnName(m).equalsIgnoreCase(
"type")) {
messagedata.append(cursor.getColumnName(m) + " : "
+ cursor.getString(m));
messagedata.append("\n");
}
}
messagedata.append("\n");
count++;
} while (cursor.moveToNext());
}
}
cursor.close();
cursor = null;
return messagedata;
}
Any idea?

Categories