Android how to get phone records, this article for everyone to announce.
Get mobile phone call recording process:
1, to obtain contentresolver;
Contentresolver resolver = Getcontentresolver ();
2, Resolver.query (*);
URI:CallLog.Calls.CONTENT_URI that require incoming call records
3, the query to obtain the cursor data acquisition.
The main code is as follows:
Mainactivity.java
Package Com.noonecode.contentresolvercalllogdemo;
Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import Java.util.Date;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import android.app.Activity;
Import Android.content.ContentResolver;
Import Android.database.Cursor;
Import Android.os.Bundle;
Import Android.provider.CallLog;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;
public class Mainactivity extends activity {private ListView mlvshow;
Private list<map<string, string>> dataList;
Private Simpleadapter adapter;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mlvshow = (ListView) Findviewbyid (r.id.lv_show);
DataList = Getdatalist (); adapter = new Simpleadapter (this, dataList, r.layout.simple_calllog_item//, new string[] {"Name", "Number", "Dat
E "," duration "," type "}//, new int[] {r.id.tv_name, r.id.tv_number, R.id.tv_date, R.id.tv_duration, r.id.tv_type});
Mlvshow.setadapter (adapter); /** * Read Data * * @return read the data/private list<map<string, string>> getdatalist () {/
1. Access to contentresolver contentresolver resolver = Getcontentresolver (); 2. Query the call log database using Contentresolver's Query method/** * @param URI requires the URI of the query (this URI is provided by ContentProvider) * @param projec
tion fields that need to be queried * @param selection The statement after the SQL statement where * @param Selectionargs? The data represented by the placeholder * @param sortOrder Sort method * * */Cursor Cursor = Resolver.query (CallLog.Calls.CONTENT_URI,//Query Call log URI new string[] {calllog.c Alls.
cached_name//Call History Contact, calllog.calls.number//call history phone number, calllog.calls.date//call record date , calllog.calls.duration//call length, CallLog.Calls.TYPE}//call type, NULL, NULL, CallLog.Calls.DEFAULT_S
ort_order//in reverse chronological order, most recently hit the first show); 3. Access to data through cursor
list<map<string, string>> list = new arraylist<map<string, string>> ();
while (Cursor.movetonext ()) {String name = cursor.getstring (Cursor.getcolumnindex (CallLog.Calls.CACHED_NAME));
String number = cursor.getstring (Cursor.getcolumnindex (CallLog.Calls.NUMBER));
Long Datelong = Cursor.getlong (Cursor.getcolumnindex (CallLog.Calls.DATE));
String date = new SimpleDateFormat ("Yyyy-mm-dd hh-mm-ss"). Format (new date (Datelong));
int duration = Cursor.getint (Cursor.getcolumnindex (CallLog.Calls.DURATION));
int type = Cursor.getint (Cursor.getcolumnindex (CallLog.Calls.TYPE));
String typestring = "";
Switch (type) {Case CallLog.Calls.INCOMING_TYPE:typeString = "enter";
Break
Case CallLog.Calls.OUTGOING_TYPE:typeString = "out";
Break
Case CallLog.Calls.MISSED_TYPE:typeString = "Not Connected";
Break
Default:break; } map<string, string&Gt
Map = new hashmap<string, string> (); Map.put ("name", (name = = null)?
"No Memo Contact": name);
Map.put ("number", number);
Map.put ("date", date);
Map.put ("duration", (DURATION/60) + "Minutes");
Map.put ("type", typestring);
List.add (map);
} return list;
}
}
Master Layout Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
tools:context= "com.noonecode.contentresolvercalllogdemo.MainActivity" >
<listview
android:id = "@+id/lv_show"
android:layout_width= "match_parent"
android:layout_height= "match_parent"/>
< /relativelayout>
Simple_calllog_item.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" a ndroid:padding= "10DP" > <textview android:id= "@+id/tv_name" android:layout_width= "Wrap_content" Andro id:layout_height= "Wrap_content" android:text= "name" android:textsize= "20sp"/> <linearlayout android: Layout_width= "Match_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <Te Xtview android:id= "@+id/tv_number" android:layout_width= wrap_content "android:layout_height=" Wrap_conte NT "android:layout_margin=" 4DP "android:text=" number "/> <textview android:id=" @+id/tv_ Date "android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:layout_margin=" 4d
P "android:text=" date " /> <textview android:id= "@+id/tv_duration" android:layout_width= "Wrap_content" Android:layo
ut_height= "Wrap_content" android:layout_margin= "4DP" android:text= "duration"/> <textview Android:id= "@+id/tv_type" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" an
Droid:layout_margin= "4DP" android:text= "type"/> </LinearLayout> </LinearLayout>
Permission to read call history:
<uses-permission android:name= "Android.permission.READ_CALL_LOG"/>
Final Effect Diagram:
Attention:
Night God simulator does not seem to call the function, do not use the night God test this example
The moderator is using the Millet 4 true machine test, USB debugging process will crash directly, need to manually in the Security Center to give access to read call records. (depending on the personal machine situation, some machines may not need to be set manually)
SOURCE Download: Http://xiazai.jb51.net/201610/yuanma/androidContentDemo (jb51.net). rar
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.