public class callhistoryhelper {
public static string getcallhistorystr(context context) {
string callhistoryjson = "";
callhistoryjson= gethostcommlog(context);
return callhistoryjson;
}
private static jsonobject getdata(cursor acursor) {
// callhistory commlogdata = new callhistory();
string date=dateformattools.dateformat(long.parselong(acursor.getstring(acursor.getcolumnindex("date"))));
long duration=acursor.getlong(acursor.getcolumnindex("duration"));
string name = acursor.getstring(acursor.getcolumnindex("name"));
if(name==null||name.equals(""))
{
name = acursor.getstring(acursor
.getcolumnindex("number"));
}
string number=acursor.getstring(acursor
.getcolumnindex("number"));
jsonobject obj=new jsonobject();
try {
obj.put("date", date);
obj.put("duration", duration);
obj.put("name", name);
obj.put("number", number);
} catch (jsonexception e) {
// todo auto-generated catch block
e.printstacktrace();
return obj;
private static string gethostcommlog(
context icontext) {
jsonobject callhistoryjson =new jsonobject();
jsonarray arrin=new jsonarray();
jsonarray arrout=new jsonarray();
jsonarray arrmiss=new jsonarray();
cursor cursor = icontext.getcontentresolver().query(
android.provider.calllog.calls.content_uri,
new string[] { "number", "name", "type", "date","duration" }, null, null,
"date desc");
while (cursor.movetonext()) {
switch (cursor.getint(cursor.getcolumnindex("type"))) {
case 1:
jsonobject obj1=getdata(cursor);
arrin.put(obj1);
break;
case 2:
jsonobject obj2=getdata(cursor);
arrout.put(obj2);
case 3:
jsonobject obj3=getdata(cursor);
arrmiss.put(obj3);
default:
}
callhistoryjson.put("incoming",arrin);
callhistoryjson.put("outgoing",arrout);
callhistoryjson.put("missed",arrmiss);
cursor.close();
return callhistoryjson.tostring();
}
轉自:http://blog.csdn.net/chaoyu168/article/details/49094005