java - How I can get the contact name from the android (CallLog.Calls.CONTENT_URI) table? -
i new android , working on application need outgoing call logs, number, call duration , name of contact. question can name , number of outgoing call calllog.calls.content_uri table of android system or need read separate table , map it. below code. 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
add permission read contacts data application manifest.
2) calling contact picker
within activity, create intent asks system find activity can perform pick action items in contacts uri.
intent intent = new intent(intent.action_pick, contactscontract.contacts.content_uri); call startactivityforresult, passing in intent (and request code integer, pick_contact in example). cause android launch activity that's registered support action_pick on people.content_uri, return activity when selection made (or canceled).
startactivityforresult(intent, pick_contact);
3) listening result
also in activity, override onactivityresult method listen return 'select contact' activity launched in step 2. should check returned request code matches value you're expecting, , result code result_ok.
you can uri of selected contact calling getdata() on data intent parameter. name of selected contact need use uri create new query , extract name 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, null, null, null); if (c.movetofirst()) { string name = c.getstring(c.getcolumnindex(contactscontract.contacts.display_name)); // todo whatever want selected contact name. } } break; } }
Comments
Post a Comment