Android Address Book Management 2 obtaining call records
The previous blog talked about getting contact information, and this blog is about getting call records.
Package cn. zxw. contact. domain;/*** call record * @ author zhan **/public class CallLogInfo {public String number; public long date; public int type; public CallLogInfo (String number, long date, int type) {super (); this. number = number; this. date = date; this. type = type;} public CallLogInfo () {super ();}}
Get call records
/*** Get all call records ** @ param context * @ return */public List
GetCallLog (Context context) {List
Infos = new ArrayList
(); ContentResolver cr = context. getContentResolver (); Uri uri = CILS. CONTENT_URI; String [] projection = new String [] {CILS. NUMBER, CILS. DATE, CILS. TYPE}; Cursor cursor = cr. query (uri, projection, null); while (cursor. moveToNext () {String number = cursor. getString (0); long date = cursor. getLong (1); int type = cursor. getInt (2); infos. add (new CallLogInfo (number, date, type);} cursor. close (); return infos ;}
Code in activity
Package cn. zxw. contact; import java. text. simpleDateFormat; import java. util. list; import cn. zxw. contact. domain. callLogInfo; import cn. zxw. contact. utils. contactsMsgUtils; import android. app. activity; import android. app. alertDialog; import android. content. context; import android. content. dialogInterface; import android. content. dialogInterface. onClickListener; import android. content. intent; import android. graphics. color; import android.net. uri; import android. OS. bundle; import android. provider. callLog. CILS; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. adapterView; import android. widget. adapterView. onItemLongClickListener; import android. widget. baseAdapter; import android. widget. listView; import android. widget. textView;/*** get call record * divided into three types based on the phone type: caller, power-off, and missed. Three different color tags are displayed * long-pressed number. In the displayed dialog box, select copy number to dial, dial-Up, send SMS * @ author zhan **/public class CallLogActivity extends Activity {private ListView lv; private MyAdapter adapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_contacts_msg_calllog); lv = (ListView) findViewById (R. id. lv); ContactsMsgUtils contactsMsgUtils = new ContactsMsgUtils (); List
Infos = contactsMsgUtils. getCallLog (this); adapter = new MyAdapter (infos); lv. setAdapter (adapter); lv. setOnItemLongClickListener (new OnItemLongClickListener () {@ Overridepublic boolean onItemLongClick
Arg0, View arg1, int arg2, long arg3) {CallLogInfo info = (CallLogInfo) adapter. getItem (arg2); final String number = info. number; String [] items = new String [] {copy the number to the dial, dial, and send a text message}; new AlertDialog. builder (CallLogActivity. this ). setTitle (operation ). setItems (items, new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {switch (which) {case 0: // copy the number to the dial startActivity (new Intent (Intent. ACTION_DIAL, Uri. parse (tel: + number); break; case 1: // dial-up permission startActivity (new Intent (Intent. ACTION_CALL, Uri. parse (tel: + number); break; case 2: // send the SMS startActivity (new Intent (Intent. ACTION_SENDTO, Uri. parse (sms: + number); break; default: break ;}}}). show (); return false ;}}) ;}// create a baseadapterprivate class MyAdapter extends BaseAdapter {private List
Infos; private LayoutInflater inflater; public MyAdapter (List
Infos) {super (); this. infos = infos; inflater = (LayoutInflater) getSystemService (Context. LAYOUT_INFLATER_SERVICE);} @ Overridepublic int getCount () {// TODO Auto-generated method stubreturn infos. size () ;}@ Overridepublic Object getItem (int position) {return infos. get (position) ;}@ Overridepublic long getItemId (int position) {return 0 ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {View view = inflater. inflate (R. layout. call_log_item, null); TextView TV _number = (TextView) view. findViewById (R. id. TV _number); TextView TV _date = (TextView) view. findViewById (R. id. TV _date); TextView TV _type = (TextView) view. findViewById (R. id. TV _type); // create an object CallLogInfo = infos. get (position); TV _number.setText (info. number); // format time SimpleDateFormat format = new SimpleDateFormat (yyyy-MM-dd hh: mm: ss); String dateStr = format. format (info. date); TV _date.setText (dateStr); String typeStr = null; int color = 0; switch (info. type) {case CILS. INCOMING_TYPE: typeStr = incoming call; color = Color. BLUE; break; case CILS. OUTGOING_TYPE: typeStr = power-off; color = Color. GREEN; break; case CILS. MISSED_TYPE: typeStr = not connected; color = Color. RED; break; default: break;} TV _type.setText (typeStr); TV _type.setTextColor (color); return view ;}}}