且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android获取通话记录

更新时间:2022-09-09 12:42:09

[java] view plain copy  print?
  1. public class CallHistoryHelper {  
  2.       
  3.     public static String getCallHistoryStr(Context context) {  
  4.         String callHistoryJson = "";  
  5.   
  6.         callHistoryJson= GetHostCommlog(context);  
  7.   
  8.         return callHistoryJson;  
  9.     }  
  10.       
  11.     private static JSONObject GetData(Cursor aCursor) {  
  12. //      CallHistory commlogdata = new CallHistory();  
  13.         String date=DateFormatTools.DateFormat(Long.parseLong(aCursor.getString(aCursor.getColumnIndex("date"))));  
  14.     Long duration=aCursor.getLong(aCursor.getColumnIndex("duration"));  
  15.         String name = aCursor.getString(aCursor.getColumnIndex("name"));  
  16.         if(name==null||name.equals(""))  
  17.         {  
  18.             name = aCursor.getString(aCursor  
  19.                     .getColumnIndex("number"));  
  20.         }  
  21.           
  22.         String number=aCursor.getString(aCursor  
  23.                 .getColumnIndex("number"));  
  24.           
  25.         JSONObject obj=new JSONObject();  
  26.           
  27.         try {  
  28.             obj.put("date", date);  
  29.             obj.put("duration", duration);  
  30.             obj.put("name", name);  
  31.             obj.put("number", number);  
  32.         } catch (JSONException e) {  
  33.             // TODO Auto-generated catch block  
  34.             e.printStackTrace();  
  35.         }  
  36.           
  37.         return obj;  
  38.     }  
  39.       
  40.     private static String GetHostCommlog(  
  41.             Context iContext) {  
  42.         JSONObject callhistoryJson =new JSONObject();  
  43.         JSONArray arrIn=new JSONArray();  
  44.         JSONArray arrOut=new JSONArray();  
  45.         JSONArray arrMiss=new JSONArray();  
  46.           
  47.         Cursor cursor = iContext.getContentResolver().query(  
  48.                 android.provider.CallLog.Calls.CONTENT_URI,  
  49.                 new String[] { "number""name""type""date","duration" }, nullnull,  
  50.                 "date DESC");  
  51.   
  52.         while (cursor.moveToNext()) {  
  53.               
  54.   
  55.             switch (cursor.getInt(cursor.getColumnIndex("type"))) {  
  56.             case 1:  
  57.                   
  58.                 JSONObject obj1=GetData(cursor);  
  59.                 arrIn.put(obj1);  
  60.                 break;  
  61.             case 2:  
  62.               
  63.                 JSONObject obj2=GetData(cursor);  
  64.                 arrOut.put(obj2);  
  65.                 break;  
  66.             case 3:  
  67.               
  68.                 JSONObject obj3=GetData(cursor);  
  69.                 arrMiss.put(obj3);  
  70.                 break;  
  71.             default:  
  72.               
  73.                 break;  
  74.             }  
  75.         }  
  76.         try {  
  77.             callhistoryJson.put("Incoming",arrIn);  
  78.             callhistoryJson.put("Outgoing",arrOut);  
  79.             callhistoryJson.put("Missed",arrMiss);  
  80.         } catch (JSONException e) {  
  81.             // TODO Auto-generated catch block  
  82.             e.printStackTrace();  
  83.         }  
  84.         cursor.close();  
  85.       
  86.         return callhistoryJson.toString();  
  87.     }  
  88.   
  89. }  


转自:http://blog.csdn.net/chaoyu168/article/details/49094005