android 擷取手機所有簡訊內容

來源:互聯網
上載者:User

原理是通過,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;}}

怎麼樣,其實就是擷取資料庫內容而以,是不是很容易,希望對大家有協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.