Accessing the SMS Inbox is another common requirement. First, you need to read the SMS permissions
- <uses-permission android:name= "Android.permission.READ_SMS"/>
Added to the description file. After you add this permission, you can read the short message in the SMS Inbox.
To read an SMS message, you must perform a query on the SMS Inbox, which is our code listing.
Layout file
- <?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>
Our Custom 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);
- }
- }
The code above opens the SMS Inbox and creates a list with each item in the list containing the body part of the SMS message. Our layout file contains only a simple TextView that contains the body of each message in the list item. To get a list of messages, you can create a URI that points to the SMS Inbox (content://sms/inbox) and then execute a simple query. The body of the SMS message is then filtered, and the list adapter for the listactivity is set. Executing the above code will see the message in the Inbox as follows.
Please make sure you have an SMS message in your inbox.
because you can access the SMS Inbox, you will be able to access other SMS-related folders, such as sent folders or the Drafts folder. The only difference between accessing the Inbox and accessing other folders is the URI specified. For example, you can execute a query against Content://sms/sent to access the Sent folder. The following is a complete list of SMS folders and a URI for each folder.
All folders: Content://sms/all
Inbox: Content://sms/inbox
emailed: Content://sms/sent
Draft: Content://sms/draft
Outbox: Content://sms/outbox
send failed: content://sms/failed
Queued messages: content://sms/queued
non-delivery: content://sms/undelivered
Dialogue: Content://sms/conversations