Android Access SMS SMS Inbox

Source: Internet
Author: User

Accessing the SMS Inbox is another common requirement. First, you need to read the SMS permissions

    1. <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

  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>



Our Custom listactivity

  1. Package Xiaohang.zhimeng;
  2. Import android.app.ListActivity;
  3. Import Android.database.Cursor;
  4. Import Android.net.Uri;
  5. Import Android.os.Bundle;
  6. Import Android.widget.ListAdapter;
  7. Import Android.widget.SimpleCursorAdapter;
  8. public class Smsinboxdemo extends Listactivity {
  9. Private ListAdapter adapter;
  10. private static final Uri Sms_inbox = Uri.parse ("Content://sms/inbox");
  11. @Override
  12. public void OnCreate (Bundle savedinstancestate) {
  13. Super.oncreate (savedinstancestate);
  14. Cursor C = getcontentresolver ()
  15. . query (Sms_inbox, NULL, NULL, NULL, NULL);
  16. Startmanagingcursor (c);
  17. string[] columns = new string[] {"Body"};
  18. int[] names = new int[] {r.id.row};
  19. adapter = new Simplecursoradapter (this, R.layout.main, C, columns,
  20. names);
  21. Setlistadapter (adapter);
  22. }
  23. }



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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.