Java – how to get the contact name from the Android (calllog. Calls. Content_uri) table?

I am new to Android and am developing an application. I need all outgoing call logs, numbers, call duration and contact names So my problem is that I can get the calllog of Android system Calls. CONTENT_ The name and number of the outgoing call to the URI table, or I need to read it from a separate table and map it Here is my code Thank you in advance

private String getCallDetails() {

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

        Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,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();

    }

Solution

You need:

1) Authority

Add permission to read contact data into the application list

2) Call contact selector

In your activity, create an intent and ask the system to find the activity that can perform pick operation from the item in the contacts URI

Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI); Call startactivityforresult and pass in this intent (and the request code integer in this example, pick_contact) This will lead to Android in people CONTENT_ Startup on URI is registered to support action_ Pick's activity, and then return to it when you make a selection (or cancel)

startActivityForResult(intent,PICK_CONTACT);

3) Listen to the results

Also in your activity, override the onactivityresult method to listen for the return of the "select contact" activity you started in step 2 You should check whether the returned request code matches your expected value and the result code is result_ OK.

You can get the URI of the selected contact by calling GetData () on the data intent parameter To get the name of the selected contact, you need to create a new query using the URI and extract the name from the returned cursor

@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);
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;
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>