android手機短信總結

  1. public String getSmsInPhone()  
  2. {  
  3.     final String SMS_URI_ALL   = "content://sms/";    
  4.     final String SMS_URI_INBOX = "content://sms/inbox";  
  5.     final String SMS_URI_SEND  = "content://sms/sent";  
  6.     final String SMS_URI_DRAFT = "content://sms/draft";  
  7.       
  8.     StringBuilder smsBuilder = new StringBuilder();  
  9.       
  10.     try{  
  11.         ContentResolver cr = getContentResolver();  
  12.         String[] projection = new String[]{"_id""address""person",   
  13.                 "body""date""type"};  
  14.         Uri uri = Uri.parse(SMS_URI_ALL);  
  15.         Cursor cur = cr.query(uri, projection, nullnull"date desc");  
  16.   
  17.         if (cur.moveToFirst()) {  
  18.             String name;   
  19.             String phoneNumber;         
  20.             String smsbody;  
  21.             String date;  
  22.             String type;  
  23.            
  24.             int nameColumn = cur.getColumnIndex("person");  
  25.             int phoneNumberColumn = cur.getColumnIndex("address");  
  26.             int smsbodyColumn = cur.getColumnIndex("body");  
  27.             int dateColumn = cur.getColumnIndex("date");  
  28.             int typeColumn = cur.getColumnIndex("type");  
  29.            
  30.             do{  
  31.                 name = cur.getString(nameColumn);               
  32.                 phoneNumber = cur.getString(phoneNumberColumn);  
  33.                 smsbody = cur.getString(smsbodyColumn);  
  34.                   
  35.                 SimpleDateFormat dateFormat = new SimpleDateFormat(  
  36.                         "yyyy-MM-dd hh:mm:ss");  
  37.                 Date d = new Date(Long.parseLong(cur.getString(dateColumn)));  
  38.                 date = dateFormat.format(d);  
  39.                   
  40.                 int typeId = cur.getInt(typeColumn);  
  41.                 if(typeId == 1){  
  42.                     type = "接收";  
  43.                 } else if(typeId == 2){  
  44.                     type = "發送";  
  45.                 } else {  
  46.                     type = "";  
  47.                 }  
  48.                
  49.                 smsBuilder.append("[");  
  50.                 smsBuilder.append(name+",");  
  51.                 smsBuilder.append(phoneNumber+",");  
  52.                 smsBuilder.append(smsbody+",");  
  53.                 smsBuilder.append(date+",");  
  54.                 smsBuilder.append(type);  
  55.                 smsBuilder.append("] ");  
  56.                
  57.                 if(smsbody == null) smsbody = "";    
  58.             }while(cur.moveToNext());  
  59.         } else {  
  60.             smsBuilder.append("no result!");  
  61.         }  
  62.            
  63.         smsBuilder.append("getSmsInPhone has executed!");  
  64.     } catch(SQLiteException ex) {  
  65.         Log.d("SQLiteException in getSmsInPhone", ex.getMessage());  
  66.     }  
  67.     return smsBuilder.toString();  
  68. }  
public String 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";
	
	StringBuilder smsBuilder = new StringBuilder();
	
	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{
				name = cur.getString(nameColumn);             
				phoneNumber = cur.getString(phoneNumberColumn);
				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 = "";
	            }
             
	            smsBuilder.append("[");
	            smsBuilder.append(name+",");
	            smsBuilder.append(phoneNumber+",");
	            smsBuilder.append(smsbody+",");
	            smsBuilder.append(date+",");
	            smsBuilder.append(type);
	            smsBuilder.append("] ");
             
				if(smsbody == null) smsbody = "";  
			}while(cur.moveToNext());
		} else {
			smsBuilder.append("no result!");
		}
		 
		smsBuilder.append("getSmsInPhone has executed!");
	} catch(SQLiteException ex) {
		Log.d("SQLiteException in getSmsInPhone", ex.getMessage());
	}
	return smsBuilder.toString();
}

代碼說明:

1、本函數用於獲取手機中所有的短信,包括收件箱、發件箱、草稿箱等。

2、本函數可以運行在Service子類中,因爲未使用Activity類的相關函數。

3、獲取的短信包括:收發短信人名、手機號碼、短信內容、短信發送接收的時間、短信的類型。

sms主要結構:

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

4、爲了獲取短信,需要在AndroidManifest.xml 文件中添加權限使用說明,如下:

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


將發送短信保存到發件箱

  1. private void storeMsg(String destinationAddress, String text)  
  2.    {  
  3.        ContentValues cv = new ContentValues();  
  4.        cv.put("address", destinationAddress);  
  5.        cv.put("person""");  
  6.        cv.put("protocol""0");  
  7.        cv.put("read""1");  
  8.        cv.put("status""-1");  
  9.        cv.put("body", text);  
  10.        this.getContentResolver().insert(Uri.parse("content://sms/sent"), cv);  
  11.        Log.e("tl3shi""msg has stored");  
  12.      
 private void storeMsg(String destinationAddress, String text)
    {
        ContentValues cv = new ContentValues();
        cv.put("address", destinationAddress);
        cv.put("person", "");
        cv.put("protocol", "0");
        cv.put("read", "1");
        cv.put("status", "-1");
        cv.put("body", text);
        this.getContentResolver().insert(Uri.parse("content://sms/sent"), cv);
        Log.e("tl3shi", "msg has stored");
    

系統會自動去匹配同一個人的會話信息。注意以上代碼權限得加讀和寫,因爲寫的時候,也讀取了數據庫中的id啊之類的字段了。

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


這是一段刪除短信會話的代碼

  1. Uri mUri=Uri.parse("content://sms/conversations/" + id);        
  2.     
  3.  mContext.getContentResolver().delete(mUri, nullnull);    
Uri mUri=Uri.parse("content://sms/conversations/" + id);      
  
 mContext.getContentResolver().delete(mUri, null, null);  


推薦文章:

android 中短信管理代碼 http://www.cnblogs.com/GnagWang/archive/2011/01/06/1929075.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章