其他学友的学习资料:
http://blog.csdn.net/wwj_748/article/details/19965913
http://uule.iteye.com/blog/1709227
http://gdgzzch.blog.163.com/blog/static/37640452201241115328383/
http://xzxjz.blog.163.com/blog/static/201031492012612115548995/
http://www.cnblogs.com/GnagWang/archive/2011/01/06/1929075.html
http://yidianfengfan.iteye.com/blog/610744
获取手机上的服务和硬件时需要先获得权限:
Androidmanifest.xml中的属性:
<!-- 读联系人权限 --> <uses-permission android:name="android.permission.READ_CONTACTS" /> <!-- 写联系人权限 --> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <!-- 拨号权限 --> <uses-permission android:name="android.permission.CALL_PHONE" /> <!-- 读短信权限 --> <uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.RECEIVE_SMS" />
在activity_main.xml中有三个按钮button:获取全部通信记录,获取指定号码通信记录,获取短信内容;一个文本显示textview:显示内容的
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="全部记录" /> <Button android:id="@+id/readone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="指定记录" /> <Button android:id="@+id/sms" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="短信记录" /> </LinearLayout> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="match_parent" android:text="通话记录列表" android:maxLines="20" android:scrollbars="vertical" android:textColor="#000000" android:fadeScrollbars="false"/> <ListView android:id="@+id/calllist" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
下面是MainActivity.class在点击button时进行显示信息:
package com.yulai.history; import java.sql.Date; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.provider.CallLog; import android.text.method.ScrollingMovementMethod; import android.view.Gravity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.Toast; import android.widget.TextView; public class MainActivity extends Activity { private ListView lvCalls = null; private Cursor cursor = null, cursorone = null,cursorsms = null; private Button btnRead = null, btnReadone,btnReadsms; final String[] PROJECT = new String[] { "_id", // 0 "name", // 1 "number", // 2 "date", // 3 "duration", // 4 "type", // 5 "new" // 6 }; StringBuilder smsBuilder1 = new StringBuilder(); TextView textView1; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lvCalls = (ListView) findViewById(R.id.calllist); btnRead = (Button) super.findViewById(R.id.read); btnReadone = (Button) super.findViewById(R.id.readone); btnReadsms = (Button) super.findViewById(R.id.sms); textView1 = (TextView) findViewById(R.id.textView1); textView1.setMovementMethod(ScrollingMovementMethod.getInstance()); btnRead.setOnClickListener(new OnClickListener() { public void onClick(View v) { cursor = MainActivity.this.getContentResolver().query( CallLog.Calls.CONTENT_URI, PROJECT, null, null, null); ArrayList<HashMap<String, String>> callLogInfoList = new ArrayList<HashMap<String, String>>(); if (null == cursor || cursor.getCount() == 0) { return; } cursor.moveToFirst(); do { HashMap<String, String> callLogInfoMap = new HashMap<String, String>(); // 格式化的效果:例如2010-01-08 09:10:11 SimpleDateFormat sfd = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); Date date = new Date(Long.parseLong(cursor.getString(cursor .getColumnIndexOrThrow(CallLog.Calls.DATE)))); String time = sfd.format(date); // 来电:1,拨出:2,未接:3 String typestrone = null; if (cursor.getString(5).equals("1")) { typestrone = "来电"; } if (cursor.getString(5).equals("2")) { typestrone = "拨出"; } if (cursor.getString(5).equals("3")) { typestrone = "未接"; } // new 1表示已读,0表示未读 String newstr = null; if (cursor.getString(5).equals("1")) { newstr = "已读"; } if (cursor.getString(5).equals("0")) { newstr = "未读"; } callLogInfoMap.put("_id", cursor.getString(0)); callLogInfoMap.put("name", cursor.getString(1)); callLogInfoMap.put("number", cursor.getString(2)); callLogInfoMap.put("date", time); callLogInfoMap.put("duration", cursor.getString(4)); // //通话时间 callLogInfoMap.put("type", typestrone); callLogInfoMap.put("new", newstr); callLogInfoList.add(callLogInfoMap); // 以下是打印信息 smsBuilder1.append(cursor.getString(2) + ","); smsBuilder1.append(cursor.getString(1) + ","); smsBuilder1.append(typestrone + ","); smsBuilder1.append(cursor.getString(4) + ","); smsBuilder1.append(time + "\n"); } while (cursor.moveToNext()); if (cursor != null) { cursor.close(); } textView1.setText(""); textView1.setText(smsBuilder1.toString()); } }); btnReadone.setOnClickListener(new OnClickListener() { public void onClick(View v) { // 查询某一个联系人的所有记录(按电话号码) cursorone = MainActivity.this.getContentResolver().query( CallLog.Calls.CONTENT_URI, null, "number=? and type=1", new String[] { "13506216991" }, null); ArrayList<HashMap<String, String>> callLogInfoList = new ArrayList<HashMap<String, String>>(); if (null == cursorone || cursorone.getCount() == 0) { Toast.makeText(MainActivity.this, "查无此人", Toast.LENGTH_SHORT).show(); textView1.setText(""); textView1.setText("没有查到有关13506216991的信息!"); return ; } cursorone.moveToFirst(); do { HashMap<String, String> callLogInfoMap = new HashMap<String, String>(); // 格式化的效果:例如2010-01-08 09:10:11 SimpleDateFormat sfd = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); Date date = new Date( Long.parseLong(cursorone.getString(cursorone .getColumnIndexOrThrow(CallLog.Calls.DATE)))); String time = sfd.format(date); // 来电:1,拨出:2,未接:3 String typestr = null; if (cursorone.getString(5).equals("1")) { typestr = "来电"; } if (cursorone.getString(5).equals("2")) { typestr = "拨出"; } if (cursorone.getString(5).equals("3")) { typestr = "未接"; } // new 1表示已读,0表示未读 String newstr = null; if (cursorone.getString(5).equals("1")) { newstr = "已读"; } if (cursorone.getString(5).equals("0")) { newstr = "未读"; } callLogInfoMap.put("_id", cursorone.getString(0)); callLogInfoMap.put("name", cursorone.getString(1)); callLogInfoMap.put("number", cursorone.getString(2)); callLogInfoMap.put("date", time); callLogInfoMap.put("duration", cursorone.getString(4)); // 通话时间 callLogInfoMap.put("type", typestr); callLogInfoMap.put("new", newstr); callLogInfoList.add(callLogInfoMap); // 以下是打印信息 smsBuilder1.append(cursorone.getString(2) + ","); smsBuilder1.append(cursorone.getString(1) + ","); smsBuilder1.append(typestr + ","); smsBuilder1.append(cursorone.getString(4) + ","); smsBuilder1.append(time + "\n"); } while (cursorone.moveToNext()); if (cursorone != null) { cursorone.close(); } textView1.setText(""); textView1.setText(smsBuilder1.toString()); } }); btnReadsms.setOnClickListener(new OnClickListener() { public void onClick(View v) { Uri uri = Uri.parse("content://sms/inbox"); String sortOrder= "date desc" ; Cursor cur = MainActivity.this.getContentResolver().query(uri, null, null, null, sortOrder); String info; HashMap<String, String> callLogInfoMap = new HashMap<String, String>(); ArrayList<HashMap<String, String>> callLogInfoList = new ArrayList<HashMap<String, String>>(); if (null == cur || cur.getCount() == 0) { return; } cur.moveToFirst(); do{ for(int j = 0; j < cur.getColumnCount(); j++){ callLogInfoMap.put(cur.getColumnName(j), cur.getString(j)); callLogInfoList.add(callLogInfoMap); smsBuilder1.append(cur.getColumnName(j) + "=" + cur.getString(j) + ","); if(cur.getColumnName(j).equals("seen")){ smsBuilder1.deleteCharAt(smsBuilder1.length()-1); smsBuilder1.append("\n\n"); } } }while(cur.moveToNext()); if (cur != null) { cur.close(); } textView1.setText(""); textView1.setText(smsBuilder1.toString()); } }); } }