android–短信查詢 – Android移動開發技術文章_手機開發 Android移動開發教學課程

sms主要結構:
_id => 短消息序號 如100 
thread_id => 對話的序號 如100 
address => 發件人地址,手機號.如+8613811810000 
person => 發件人,返回一個數字就是聯系人列表裡的序號,陌生人為null 
date => 日期  long型。如1256539465022 
protocol => 協議 0 SMS_RPOTO, 1 MMS_PROTO  
read => 是否閱讀 0未讀, 1已讀  
status => 狀態 -1接收,0 complete, 64 pending, 128 failed  
type => 類型 1是接收到的,2是已發出  
body => 短消息內容  
service_center => 短信服務中心號碼編號。如+8613800755500 


查詢短信是通過contentprovider實現的
 例如  查詢收件箱:managedQuery(Uri.parse("content://sms/inbox");
查詢發件箱 managedQuery(Uri.parse("content://sms/send");


Java代碼

public final static String SMS_URI_ALL =  "content://sms/"; //0 
public final static String SMS_URI_INBOX = "content://sms/inbox";//1 
public final static String SMS_URI_SEND = "content://sms/sent";//2 
public final static String SMS_URI_DRAFT = "content://sms/draft";//3 
public final static String SMS_URI_OUTBOX = "content://sms/outbox";//4 
public final static String SMS_URI_FAILED = "content://sms/failed";//5 
public final static String SMS_URI_QUEUED = "content://sms/queued";//6 


例子:
view plaincopy to clipboardprint?package com.shao.sms; 
 
import android.app.Activity; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteException; 
import android.net.Uri; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 
 
public class ReadSMSActivity extends Activity { 
/** Called when the activity is first created. */ 
private static final String LOG_TAG = "Sms Query"; 
 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
   super.onCreate(savedInstanceState);  
   TextView tv = (TextView) findViewById(R.id.text); 
 
   tv.setText(""); 
   tv.setText(getSmsAndSendBack()); 

/**
* 讀取短信
* @return
*/ 
public String getSmsAndSendBack() 

   String[] projection = new String[] { 
     "_id", 
     "address", 
     "person", 
     "body", 
     "type" 
    }; 
   StringBuilder str=new StringBuilder(); 
   try{ 
    Cursor myCursor = managedQuery(Uri.parse("content://sms"), 
      projection, 
      null, null , "date desc"); 
    str.append(processResults(myCursor));   
   } 
   catch (SQLiteException ex) 
   { 
    Log.d(LOG_TAG, ex.getMessage()); 
   } 
   return str.toString(); 

/**
* 處理短信結果
*
*/ 
private StringBuilder processResults(Cursor cur) { 
   // TODO Auto-generated method stub  
   StringBuilder sb=new StringBuilder(); 
   if (cur.moveToFirst()) { 
 
         String name;  
         String phoneNumber;        
         String sms; 
         int type; 
          
         int nameColumn = cur.getColumnIndex("person"); 
         int phoneColumn = cur.getColumnIndex("address"); 
         int smsColumn = cur.getColumnIndex("body"); 
         int typeColum = cur.getColumnIndex("type"); 
          
         do { 
             // Get the field values  
             name = cur.getString(nameColumn);              
             phoneNumber = cur.getString(phoneColumn); 
             sms = cur.getString(smsColumn); 
             type = cur.getInt(typeColum); 
             
             System.out.println("……………………………."); 
             System.out.println("name"+name); 
             System.out.println("type"+type); 
             System.out.println("phoneNumber"+phoneNumber); 
             System.out.println("sms"+sms); 
             System.out.println("……………………………."); 
             sb.append("{"); 
             sb.append(name+","); 
             sb.append(phoneNumber+","); 
             sb.append(sms); 
             sb.append("}"); 
             if (null==sms) 
             sms=""; 
         } while (cur.moveToNext()); 
     } 
     else 
     { 
     sb.append("no result!"); 
     } 
 
   return sb; 


package com.shao.sms;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class ReadSMSActivity extends Activity {
/** Called when the activity is first created. */
private static final String LOG_TAG = "Sms Query";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   super.onCreate(savedInstanceState);
   TextView tv = (TextView) findViewById(R.id.text);

   tv.setText("");
   tv.setText(getSmsAndSendBack());
}
/**
* 讀取短信
* @return
*/
public String getSmsAndSendBack()
{
   String[] projection = new String[] {
     "_id",
     "address",
     "person",
     "body",
     "type"
    };
   StringBuilder str=new StringBuilder();
   try{
    Cursor myCursor = managedQuery(Uri.parse("content://sms"),
      projection,
      null, null , "date desc");
    str.append(processResults(myCursor)); 
   }
   catch (SQLiteException ex)
   {
    Log.d(LOG_TAG, ex.getMessage());
   }
   return str.toString();
}
/**
* 處理短信結果
*
*/
private StringBuilder processResults(Cursor cur) {
   // TODO Auto-generated method stub
   StringBuilder sb=new StringBuilder();
   if (cur.moveToFirst()) {

         String name;
         String phoneNumber;      
         String sms;
         int type;
        
         int nameColumn = cur.getColumnIndex("person");
         int phoneColumn = cur.getColumnIndex("address");
         int smsColumn = cur.getColumnIndex("body");
         int typeColum = cur.getColumnIndex("type");
        
         do {
             // Get the field values
             name = cur.getString(nameColumn);            
             phoneNumber = cur.getString(phoneColumn);
             sms = cur.getString(smsColumn);
             type = cur.getInt(typeColum);
           
             System.out.println("…………………………….");
             System.out.println("name"+name);
             System.out.println("type"+type);
             System.out.println("phoneNumber"+phoneNumber);
             System.out.println("sms"+sms);
             System.out.println("…………………………….");
             sb.append("{");
             sb.append(name+",");
             sb.append(phoneNumber+",");
             sb.append(sms);
             sb.append("}");
             if (null==sms)
             sms="";
         } while (cur.moveToNext());
     }
     else
     {
     sb.append("no result!");
     }

   return sb;
}
}


記得在AndroidManifest.xml中加入android.permission.READ_SMS這個permission

<uses-permission android:name="android.permission.READ_SMS" />


 作者“一個人的ye”
 

發佈留言