其餘學友的學習資料:html
http://blog.csdn.net/wwj_748/article/details/19965913java
http://uule.iteye.com/blog/1709227android
http://gdgzzch.blog.163.com/blog/static/37640452201241115328383/sql
http://xzxjz.blog.163.com/blog/static/201031492012612115548995/app
http://www.cnblogs.com/GnagWang/archive/2011/01/06/1929075.htmlide
http://yidianfengfan.iteye.com/blog/610744學習
獲取手機上的服務和硬件時須要先得到權限:ui
Androidmanifest.xml中的屬性:this
<!-- 讀聯繫人權限 --> <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:顯示內容的spa
<?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()); } }); } }