原理是通過,contentprovider擷取系統簡訊資料庫中的欄位資訊而達到擷取內容目的
如下:
具體代碼如下:
package com.internal.message;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.ListActivity;import android.content.ContentResolver;import android.database.Cursor;import android.database.sqlite.SQLiteException;import android.net.Uri;import android.os.Bundle;import android.provider.ContactsContract;import android.provider.ContactsContract.CommonDataKinds.Phone;import android.provider.ContactsContract.PhoneLookup;import android.util.Log;import android.widget.ListView;import android.widget.SimpleAdapter;public class QureSms extends ListActivity {ListView smslist=null;//顯示列表資訊ArrayList<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();List<String> title=new ArrayList<String>();//簡訊來源List<String> text=new ArrayList<String>();//簡訊內容 @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);smslist=getListView();getSmsInPhone();int lengh = title.size();for(int i =0; i < lengh; i++) { Map<String,Object> item = new HashMap<String,Object>(); item.put("title", title.get(i)); item.put("text", text.get(i)); mData.add(item); }SimpleAdapter adapter = new SimpleAdapter(this,mData,android.R.layout.simple_list_item_2,new String[]{"title","text"},new int[]{android.R.id.text1,android.R.id.text2});setListAdapter(adapter);}/** * 擷取手機內所以短訊息 */private void getSmsInPhone(){ final String SMS_URI_ALL = "content://sms/"; /*final String SMS_URI_INBOX = "content://sms/inbox"; final String SMS_URI_SEND = "content://sms/sent"; final String SMS_URI_DRAFT = "content://sms/draft"; */ try{ ContentResolver cr = getContentResolver(); String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"}; Uri uri = Uri.parse(SMS_URI_ALL); Cursor cur = cr.query(uri, projection, null, null, "date desc"); if (cur.moveToFirst()) { String name; String phoneNumber; String smsbody; String date; String type; // int nameColumn = cur.getColumnIndex("person"); int phoneNumberColumn = cur.getColumnIndex("address"); int smsbodyColumn = cur.getColumnIndex("body"); int dateColumn = cur.getColumnIndex("date"); int typeColumn = cur.getColumnIndex("type"); do{ phoneNumber = cur.getString(phoneNumberColumn); // name = cur.getString(nameColumn); 這樣擷取的聯絡認為空白,所以我改用下面的方法擷取 name=getPeopleNameFromPerson(phoneNumber); smsbody = cur.getString(smsbodyColumn); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); Date d = new Date(Long.parseLong(cur.getString(dateColumn))); date = dateFormat.format(d); int typeId = cur.getInt(typeColumn); if(typeId == 1){ type = "接收"; } else if(typeId == 2){ type = "發送"; } else { type = "草稿"; } title.add(type+" "+date+'\n'+phoneNumber); text.add(name+'\n'+smsbody); if(smsbody == null) smsbody = ""; } while(cur.moveToNext()); } cur.close(); cur=null; } catch(SQLiteException ex) { Log.e("SQLiteException in getSmsInPhone", ex.getMessage()); } } /** * 通過address手機號關聯Contacts連絡人的顯示名字 * @param address * @return */private String getPeopleNameFromPerson(String address){if(address == null || address == ""){return null;}String strPerson = "null";String[] projection = new String[] {Phone.DISPLAY_NAME, Phone.NUMBER};Uri uri_Person = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, address);// address 手機號過濾Cursor cursor = getContentResolver().query(uri_Person, projection, null, null, null);if(cursor.moveToFirst()){int index_PeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME);String strPeopleName = cursor.getString(index_PeopleName);strPerson = strPeopleName;}else{strPerson = address;}cursor.close();cursor=null;return strPerson;}}
怎麼樣,其實就是擷取資料庫內容而以,是不是很容易,希望對大家有協助。