標籤:android http get width 檔案 os
訪問 SMS收件匣是另一個常見的需求。首先,需要將讀取 SMS 的許可權
- <uses-permission android:name="android.permission.READ_SMS"/>
添加到描述檔案中。添加此許可權後就可以讀取SMS收件匣中的 短訊息了。
要讀取 SMS 訊息,必須對SMS收件匣執行查詢,下面是我們的 代碼清單。
布局檔案
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/row"
- android:layout_width="180dip"
- android:layout_height="30dip"
- android:textSize="10pt"
- android:singleLine="true"
- />
- </LinearLayout>
我們自訂的ListActivity
- package xiaohang.zhimeng;
-
- import android.app.ListActivity;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.widget.ListAdapter;
- import android.widget.SimpleCursorAdapter;
-
- public class SMSINboxDemo extends ListActivity {
- private ListAdapter adapter;
- private static final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Cursor c = getContentResolver()
- .query(SMS_INBOX, null, null, null, null);
- startManagingCursor(c);
- String[] columns = new String[] { "body" };
- int[] names = new int[] { R.id.row };
- adapter = new SimpleCursorAdapter(this, R.layout.main, c, columns,
- names);
- setListAdapter(adapter);
- }
- }
上面的代碼開啟 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