獲取手機短信內容
原理是通過,contentprovider獲取係統短信數據庫中的字段信息而達到獲取內容目的
效果圖如下:
具體代碼如下:
[html]
view plaincopy
- 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;
- }
- }
最後更新:2017-04-03 12:56:08