我如何从android(CallLog.Calls.CONTENT_URI)表中获取联系人姓名?

时间:2016-01-28 12:05:32

标签: java android

我是Android的新手并且在应用程序上工作,我需要所有传出呼叫日志,号码,呼叫持续时间和联系人姓名。所以我的问题是我可以获取android系统的CallLog.Calls.CONTENT_URI表的传出调用的名称和编号,或者我需要从单独的表中读取它并映射它。以下是我的代码。提前谢谢。

private String getCallDetails() {

        StringBuffer sb = new StringBuffer();
        // Cursor managedCursor =
        // getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
        // null, null, null);

        Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, CallLog.Calls.DATE + ">?",
                new String[] { String.valueOf("1451586601000") }, CallLog.Calls.NUMBER + " asc");
        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);
        int name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
        // int geoCodeColumn =
        // managedCursor.getColumnIndex(CallLog.Calls.GEOCODED_LOCATION);

        // sb.append("Call Details :");
        while (managedCursor.moveToNext()) {
            String phNumber = managedCursor.getString(number);
            String callType = managedCursor.getString(type);
            String callDate = managedCursor.getString(date);
            String callerName = managedCursor.getString(name);
            // long calldate_timeStamp= Long.parseLong(callDate);
            // long temp_time = 1451586601000L;
            // if(calldate_timeStamp>temp_time){
            // String geoCode = managedCursor.getString(geoCodeColumn);
            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";
                int total_call_duration = Integer.parseInt(callDuration);
                total_time = total_time + total_call_duration;
                MyContact dialedContact = new MyContact();
                dialedContact.setPhoneNumber(Long.parseLong(phNumber));
                dialedContact.setCallDuration(Integer.parseInt(callDuration));
                // dialedContact.se
                 sb.append("\nPhone Number:--- " + phNumber + " \nCallType:--- "
                 + dir + " \nCall Date:--- " + callDayTime
                 + " \nCall duration in sec :--- " + callDuration+ " \nGeocode: " );
                 sb.append("\n----------------------------------");

                break;

            case CallLog.Calls.INCOMING_TYPE:
                dir = "INCOMING";
                break;

            case CallLog.Calls.MISSED_TYPE:
                dir = "MISSED";
                break;
            }
        }

        // }
        managedCursor.close();
    //  sb.append("" + total_time / 60);// call duration in minute
        return sb.toString();

    }

1 个答案:

答案 0 :(得分:0)

您需要:

1)权限

添加将联系人数据读取到应用程序清单的权限。

2)致电联系人选择

在您的Activity中,创建一个Intent,要求系统查找可以从Contacts URI中的项目执行PICK操作的Activity。

Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI); 调用startActivityForResult,传入此Intent(以及此示例中的请求代码整数,PICK_CONTACT)。这将导致Android在People.CONTENT_URI上启动已注册为支持ACTION_PICK的活动,然后在进行选择(或取消)时返回此活动。

startActivityForResult(intent, PICK_CONTACT);

3)听取结果

同样在您的Activity中,覆盖onActivityResult方法以侦听您在步骤2中启动的“选择联系人”活动的返回。您应该检查返回的请求代码是否与您期望的值匹配,并且结果代码是RESULT_OK。

您可以通过对数据Intent参数调用getData()来获取所选联系人的URI。要获取所选联系人的名称,您需要使用该URI创建新查询并从返回的游标中提取名称。

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
  super.onActivityResult(reqCode, resultCode, data); 
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c =  getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Whatever you want to do with the selected contact name. 
    }
}
break;
    }
}