android 訪問SMS簡訊收件匣

來源:互聯網
上載者:User

標籤:android   http   get   width   檔案   os   

訪問 SMS收件匣是另一個常見的需求。首先,需要將讀取 SMS 的許可權 

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

添加到描述檔案中。添加此許可權後就可以讀取SMS收件匣中的 短訊息了。 

    要讀取 SMS 訊息,必須對SMS收件匣執行查詢,下面是我們的 代碼清單。 

    布局檔案 

 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.       <TextView    
  8.          android:id="@+id/row"    
  9.          android:layout_width="180dip"    
  10.          android:layout_height="30dip"    
  11.          android:textSize="10pt"    
  12.          android:singleLine="true"    
  13.      />   
  14. </LinearLayout>  



   我們自訂的ListActivity 
  

 
  1. package xiaohang.zhimeng;  
  2.   
  3. import android.app.ListActivity;  
  4. import android.database.Cursor;  
  5. import android.net.Uri;  
  6. import android.os.Bundle;  
  7. import android.widget.ListAdapter;  
  8. import android.widget.SimpleCursorAdapter;  
  9.   
  10. public class SMSINboxDemo extends ListActivity {  
  11.     private ListAdapter adapter;  
  12.     private static final Uri SMS_INBOX = Uri.parse("content://sms/inbox");  
  13.   
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         Cursor c = getContentResolver()  
  18.                 .query(SMS_INBOX, null, null, null, null);  
  19.         startManagingCursor(c);  
  20.         String[] columns = new String[] { "body" };  
  21.         int[] names = new int[] { R.id.row };  
  22.         adapter = new SimpleCursorAdapter(this, R.layout.main, c, columns,  
  23.                 names);  
  24.         setListAdapter(adapter);  
  25.     }  
  26. }  



    上面的代碼開啟 SMS收件匣並建立了一個列表,列表中的每一項都包含 SMS訊息的本文部分。我們的布局檔案就只包含了一個簡單的 TextView,它包含清單項目中每條訊息的本文。要獲得訊息列表,可以建立指向 SMS收件匣的 URI (content://sms/inbox),然後執行簡單查詢。然後對 SMS訊息的本文進行過濾,並設定  ListActivity的列表 適配器。執行上面的代碼將看到收件匣中的訊息 , 如下。 

  

  請大家確保自己的收件匣中有 SMS訊息。 

    因為可以訪問SMS收件匣,所以將能夠訪問其他與SMS 相關的檔案夾,比如已傳送檔案夾或草稿箱檔案夾。訪問收件匣與訪問其它檔案夾的唯一區別就在於所指定的 URI。例如,可以對 content://sms/sent 執行查詢來訪問已發送的檔案夾。以下是完整的 SMS資料夾清單和每個檔案夾的URI。 
    
    所有檔案夾:content://sms/all 
   收件匣:content://sms/inbox 
   已發送:content://sms/sent 
   草稿:content://sms/draft 
   寄件匣:content://sms/outbox 
   發送失敗:content://sms/failed 
   排隊訊息:content://sms/queued 
   未送達:content://sms/undelivered 

   對話:content://sms/conversations 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.