如何访问Android的通话logging?

我想收到通话logging。 例如,用户的呼叫次数,被呼叫的分钟数等

我如何在android中实现这个?

Related of "如何访问Android的通话logging?"

这是用于访问电话logging:

从Jellybean(4.1)开始,您需要以下权限:
<uses-permission android:name="android.permission.READ_CALL_LOG" />

码:

  Uri allCalls = Uri.parse("content://call_log/calls"); Cursor c = managedQuery(allCalls, null, null, null, null); String num= c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));// for number String name= c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));// for name String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));// for duration int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));// for call type, Incoming or out going. 

这是用来获取通话logging的方法。 只要把这个方法放在课堂上,就可以获得通话logging列表。

看看这个

 private String getCallDetails() { StringBuffer sb = new StringBuffer(); Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, null); int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER); int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE); int date = managedCursor.getColumnIndex(CallLog.Calls.DATE); int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION); sb.append("Call Details :"); while (managedCursor.moveToNext()) { String phNumber = managedCursor.getString(number); String callType = managedCursor.getString(type); String callDate = managedCursor.getString(date); Date callDayTime = new Date(Long.valueOf(callDate)); String callDuration = managedCursor.getString(duration); String dir = null; int dircode = Integer.parseInt(callType); switch (dircode) { case CallLog.Calls.OUTGOING_TYPE: dir = "OUTGOING"; break; case CallLog.Calls.INCOMING_TYPE: dir = "INCOMING"; break; case CallLog.Calls.MISSED_TYPE: dir = "MISSED"; break; } sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration); sb.append("\n----------------------------------"); } managedCursor.close(); return sb.toString(); } 

输出看起来

在这里输入图像说明

从任何地方使用这种方法

 private static String getCallDetails(Context context) { StringBuffer stringBuffer = new StringBuffer(); Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " DESC"); int number = cursor.getColumnIndex(CallLog.Calls.NUMBER); int type = cursor.getColumnIndex(CallLog.Calls.TYPE); int date = cursor.getColumnIndex(CallLog.Calls.DATE); int duration = cursor.getColumnIndex(CallLog.Calls.DURATION); while (cursor.moveToNext()) { String phNumber = cursor.getString(number); String callType = cursor.getString(type); String callDate = cursor.getString(date); Date callDayTime = new Date(Long.valueOf(callDate)); String callDuration = cursor.getString(duration); String dir = null; int dircode = Integer.parseInt(callType); switch (dircode) { case CallLog.Calls.OUTGOING_TYPE: dir = "OUTGOING"; break; case CallLog.Calls.INCOMING_TYPE: dir = "INCOMING"; break; case CallLog.Calls.MISSED_TYPE: dir = "MISSED"; break; } stringBuffer.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration); stringBuffer.append("\n----------------------------------"); } cursor.close(); return stringBuffer.toString(); } 

这篇文章有点旧,但这里是另一个简单的解决scheme,获取有关Android的Calllogging内容提供者的数据:

使用这个lib: https : //github.com/EverythingMe/easy-content-providers

获取所有电话:

 CallsProvider callsProvider = new CallsProvider(context); List<Call> calls = callsProvider.getCalls().getList(); 

每个呼叫都有所有的字段,所以你可以得到你需要的任何信息:
callDate,持续时间,数量,types(INCOMING,OUTGOING,MISSED),isRead,…

它适用于ListCursor并有一个示例应用程序,看看它的外观和工作。

实际上,所有Android内容提供商都支持: 联系人,短信,日历,…所有选项的完整文档: https : //github.com/EverythingMe/easy-content-providers/wiki/Android-providers

希望它也帮助:)

在我的项目我越来越错误int htc device.now这个代码是通用的。 我认为这对你有帮助。

  public class CustomContentObserver extends ContentObserver { public CustomContentObserver(Handler handler) { super(handler); System.out.println("Content obser"); } public void onChange(boolean selfChange) { super.onChange(selfChange); String lastCallnumber; currentDate = sdfcur.format(calender.getTime()); System.out.println("Content obser onChange()"); Log.d("PhoneService", "custom StringsContentObserver.onChange( " + selfChange + ")"); //if(!callFlag){ String[] projection = new String[]{CallLog.Calls.NUMBER, CallLog.Calls.TYPE, CallLog.Calls.DURATION, CallLog.Calls.CACHED_NAME, CallLog.Calls._ID}; Cursor c; c=mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, null, CallLog.Calls._ID + " DESC"); if(c.getCount()!=0){ c.moveToFirst(); lastCallnumber = c.getString(0); String type=c.getString(1); String duration=c.getString(2); String name=c.getString(3); String id=c.getString(4); System.out.println("CALLLLing:"+lastCallnumber+"Type:"+type); Database db=new Database(mContext); Cursor cur =db.getFirstRecord(lastCallnumber); final String endCall=lastCallnumber; //checking incoming/outgoing call if(type.equals("3")){ //missed call }else if(type.equals("1")){ //incoming call }else if(type.equals("2")){ //outgoing call } } c.close(); } } 

要获取只有来电logging,下方的代码将帮助你:)

 private void getCallDetailsAgil() { StringBuffer sb = new StringBuffer(); Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, null); int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER); int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE); int date = managedCursor.getColumnIndex(CallLog.Calls.DATE); int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION); sb.append("Call Details :"); while (managedCursor.moveToNext()) { String phNumber = managedCursor.getString(number); String callType = managedCursor.getString(type); String callDate = managedCursor.getString(date); Date callDayTime = new Date(Long.valueOf(callDate)); String callDuration = managedCursor.getString(duration); String dir = null; int dircode = Integer.parseInt(callType); switch (dircode) { case CallLog.Calls.OUTGOING_TYPE: dir = "OUTGOING"; break; case CallLog.Calls.INCOMING_TYPE: dir = "INCOMING"; sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration); sb.append("\n----------------------------------"); miss_cal.setText(sb); break; case CallLog.Calls.MISSED_TYPE: dir = "MISSED"; break; } } managedCursor.close(); }