原理是通过,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>(); //短信内容 @Override protected 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; } }
怎么样,其实就是获取数据库内容而以,是不是很容易,希望对大家有帮助。