Android gets the talk time instance analysis

  • 2020-05-07 20:24:21
  • OfStack

This article summarizes 1 piece of Android to get the talk time program code, there is a need for friends to refer to 1.

We know that the amount of time you talk on android is Callog, so we recommend checking ContactProvider or TelephonyProvider

Service tests

Enable Service at the beginning of a call to record the current time A, then stopSelf(); In addition, start Service again at the end of the call to get the current time B again, and then compare the time A and B

String time = Long. toString

then calls


Toast.makeText(this, time, Toast.LENGTH_SHORT).show(); 

Make it show, stopSelf();

Gets the length of the contact's call time java code

Cursor cursor = getContentResolver().query(Calls.CONTENT_URI, 
new String[] { Calls.DURATION, Calls.TYPE, Calls.DATE }, 
null, 
null, 
Calls.DEFAULT_SORT_ORDER); 
MainActivity.this.startManagingCursor(cursor); 
boolean hasRecord = cursor.moveToFirst(); 
long incoming = 0L; 
long outgoing = 0L; 
int count = 0; 
while (hasRecord) { 
int type = cursor.getInt(cursor.getColumnIndex(Calls.TYPE)); 
long duration = cursor.getLong(cursor.getColumnIndex(Calls.DURATION)); 
switch (type) { 
case Calls.INCOMING_TYPE: 
incoming += duration; 
break; 
case Calls.OUTGOING_TYPE: 
outgoing += duration; 
default: 
break; 
} 
count++; 
hasRecord = cursor.moveToNext(); 
} 
Toast.makeText(MainActivity.this, 
" A total of  " + count + " call  .  Total call time  " + (incoming + outgoing) + " seconds .  One answer  " + incoming + "  seconds ,  dialing  " 
+ outgoing + "  seconds .", 
Toast.LENGTH_LONG).show();


Related articles: