To obtain call records, use CallLog.
Key part of the java file:
Private ContentResolver cr;
Private Cursor cursor;
Private String LOG = "mobilecils ";
String str = "call history: \ n ";
Public void setContenResolver (){
Int I = 0;
SetCr (context. getContentResolver ());
SetCursor (getCr (). query (CallLog. CILS. CONTENT_URI, // use the system URI to obtain call records
New String [] {CallLog. CILS. NUMBER, // phone NUMBER
CallLog. CILS. CACHED_NAME, // contact
CallLog. CILS. TYPE, // call TYPE
CallLog. CILS. DATE, // call time
CallLog. CILS. DURATION // call DURATION
}, Null, null, CallLog. CILS. DEFAULT_SORT_ORDER ));
// Traverse each call record
For (cursor. moveToFirst ();! Cursor. isAfterLast () & I <100; cursor
. MoveToNext ()){
String strNumber = cursor. getString (0); // call number
String strName = cursor. getString (1); // contact name
Int type = cursor. getInt (2 );
String str_type = "";
If (type = CallLog. CILS. INCOMING_TYPE ){
Str_type = "Incoming ";
} Elseif (type = CallLog. CILS. OUTGOING_TYPE ){
Str_type = "outgoing call ";
} Elseif (type = CallLog. CILS. MISSED_TYPE ){
Str_type = "not connected ";
}
Long duration = cursor. getLong (4 );
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
Date date = new Date (Long. parseLong (cursor. getString (3 )));
String time = sdf. format (date );
Log. I (LOG, "\ n type:" + str_type + "\ n name:" + strName + "\ n number :"
+ StrNumber + "\ n call duration:" + duration + "second" + "\ n time:" + time
+ "\ N --------------------- \ n ");
I ++;
}
}
Of course, you must apply for the following permissions in MainFest. xml:
Then you can view the phone call history!