简体   繁体   English

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

[英]How I can get the contact name from the android (CallLog.Calls.CONTENT_URI) table?

I am new to android and working on an application where I need the all the outgoing call logs, number, call duration and Name of the contact. 我是Android的新手并且正在开发一个应用程序,我需要所有传出呼叫日志,号码,呼叫持续时间和联系人姓名。 So my question is can I get the Name and number of the outgoing call for the CallLog.Calls.CONTENT_URI table of android system or I need to read it from separate table and map it. 所以我的问题是我可以获取android系统的CallLog.Calls.CONTENT_URI表的传出调用的名称和编号,或者我需要从单独的表中读取它并映射它。 Below is my code. 以下是我的代码。 Thanks in advance. 提前致谢。

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();

    }

You need: 你需要:

1) Permissions 1)权限

Add a permission to read contacts data to your application manifest. 添加将联系人数据读取到应用程序清单的权限。

2) Calling the Contact Picker 2)呼叫联系人选择器

Within your Activity, create an Intent that asks the system to find an Activity that can perform a PICK action from the items in the Contacts URI. 在您的Activity中,创建一个Intent,要求系统查找可以从Contacts URI中的项目执行PICK操作的Activity。

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI); Call startActivityForResult, passing in this Intent (and a request code integer, PICK_CONTACT in this example). 调用startActivityForResult,传入此Intent(以及此示例中的请求代码整数,PICK_CONTACT)。 This will cause Android to launch an Activity that's registered to support ACTION_PICK on the People.CONTENT_URI, then return to this Activity when the selection is made (or canceled). 这将导致Android在People.CONTENT_URI上启动已注册为支持ACTION_PICK的活动,然后在进行选择(或取消)时返回此活动。

startActivityForResult(intent, PICK_CONTACT);

3) Listening for the Result 3)听取结果

Also in your Activity, override the onActivityResult method to listen for the return from the 'select a contact' Activity you launched in step 2. You should check that the returned request code matches the value you're expecting, and that the result code is RESULT_OK. 同样在您的Activity中,覆盖onActivityResult方法以侦听您在步骤2中启动的“选择联系人”活动的返回。您应该检查返回的请求代码是否与您期望的值匹配,并且结果代码是RESULT_OK。

You can get the URI of the selected contact by calling getData() on the data Intent parameter. 您可以通过在数据Intent参数上调用getData()来获取所选联系人的URI。 To get the name of the selected contact you need to use that URI to create a new query and extract the name from the returned cursor. 要获取所选联系人的名称,您需要使用该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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM