Main Structure of sms:
_ Id => short message number, for example, 100
Thread_id => the dialog number, for example, 100
Address => sender address, mobile phone number. For example, + 8613811810000
Person => sender. A number is returned, which is the serial number in the contact list. The stranger is null.
Date => long type. For example, 1256539465022
Protocol => protocol 0 SMS_RPOTO, 1 MMS_PROTO
Read => whether to read 0 unread, 1 read
Status => status-1 received, 0 complete, 64 pending, 128 failed
Type => type 1 is received, and 2 is sent
Body => Short Message Content
Service_center => Number of the SMS service center. For example, + 8613800755500
You can use contentprovider to query text messages.
For example, query the inbox: managedQuery (Uri. parse ("content: // sms/inbox ");
Query the sender managedQuery (Uri. parse ("content: // sms/send ");
Java code
Public final static String SMS_URI_ALL = "content: // sms/"; // 0
Public final static String SMS_URI_INBOX = "content: // sms/inbox"; // 1
Public final static String SMS_URI_SEND = "content: // sms/sent"; // 2
Public final static String SMS_URI_DRAFT = "content: // sms/draft"; // 3
Public final static String SMS_URI_OUTBOX = "content: // sms/outbox"; // 4
Public final static String SMS_URI_FAILED = "content: // sms/failed"; // 5
Public final static String SMS_URI_QUEUED = "content: // sms/queued"; // 6
Example:
View plaincopy to clipboardprint? Package com. shao. sms;
Import android. app. Activity;
Import android. database. Cursor;
Import android. database. sqlite. SQLiteException;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. util. Log;
Import android. widget. TextView;
Public class ReadSMSActivity extends Activity {
/** Called when the activity is first created .*/
Private static final String LOG_TAG = "Sms Query ";
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Super. onCreate (savedInstanceState );
TextView TV = (TextView) findViewById (R. id. text );
TV. setText ("");
TV. setText (getSmsAndSendBack ());
}
/**
* Reading text messages
* @ Return
*/
Public String getSmsAndSendBack ()
{
String [] projection = new String [] {
"_ Id ",
"Address ",
"Person ",
"Body ",
"Type"
};
StringBuilder str = new StringBuilder ();
Try {
Cursor myCursor = managedQuery (Uri. parse ("content: // sms "),
Projection,
Null, null, "date desc ");
Str. append (processResults (myCursor ));
}
Catch (SQLiteException ex)
{
Log. d (LOG_TAG, ex. getMessage ());
}
Return str. toString ();
}
/**
* Process SMS results
*
*/
Private StringBuilder processResults (Cursor cur ){
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder ();
If (cur. moveToFirst ()){
String name;
String phoneNumber;
String sms;
Int type;
Int nameColumn = cur. getColumnIndex ("person ");
Int phoneColumn = cur. getColumnIndex ("address ");
Int smsColumn = cur. getColumnIndex ("body ");
Int typeColum = cur. getColumnIndex ("type ");
Do {
// Get the field values
Name = cur. getString (nameColumn );
PhoneNumber = cur. getString (phoneColumn );
Sms = cur. getString (smsColumn );
Type = cur. getInt (typeColum );
System. out. println (".................................. ");
System. out. println ("name" + name );
System. out. println ("type" + type );
System. out. println ("phoneNumber" + phoneNumber );
System. out. println ("sms" + sms );
System. out. println (".................................. ");
Sb. append ("{");
Sb. append (name + ",");
Sb. append (phoneNumber + ",");
Sb. append (sms );
Sb. append ("}");
If (null = sms)
Sms = "";
} While (cur. moveToNext ());
}
Else
{
Sb. append ("no result! ");
}
Return sb;
}
}
Package com. shao. sms;
Import android. app. Activity;
Import android. database. Cursor;
Import android. database. sqlite. SQLiteException;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. util. Log;
Import android. widget. TextView;
Public class ReadSMSActivity extends Activity {
/** Called when the activity is first created .*/
Private static final String LOG_TAG = "Sms Query ";
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Super. onCreate (savedInstanceState );
TextView TV = (TextView) findViewById (R. id. text );
TV. setText ("");
TV. setText (getSmsAndSendBack ());
}
/**
* Reading text messages
* @ Return
*/
Public String getSmsAndSendBack ()
{
String [] projection = new String [] {
"_ Id ",
"Address ",
"Person ",
"Body ",
"Type"
};
StringBuilder str = new StringBuilder ();
Try {
Cursor myCursor = managedQuery (Uri. parse ("content: // sms "),
Projection,
Null, null, "date desc ");
Str. append (processResults (myCursor ));
}
Catch (SQLiteException ex)
{
Log. d (LOG_TAG, ex. getMessage ());
}
Return str. toString ();
}
/**
* Process SMS results
*
*/
Private StringBuilder processResults (Cursor cur ){
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder ();
If (cur. moveToFirst ()){
String name;
String phoneNumber;
String sms;
Int type;
Int nameColumn = cur. getColumnIndex ("person ");
Int phoneColumn = cur. getColumnIndex ("address ");
Int smsColumn = cur. getColumnIndex ("body ");
Int typeColum = cur. getColumnIndex ("type ");
Do {
// Get the field values
Name = cur. getString (nameColumn );
PhoneNumber = cur. getString (phoneColumn );
Sms = cur. getString (smsColumn );
Type = cur. getInt (typeColum );
System. out. println (".................................. ");
System. out. println ("name" + name );
System. out. println ("type" + type );
System. out. println ("phoneNumber" + phoneNumber );
System. out. println ("sms" + sms );
System. out. println (".................................. ");
Sb. append ("{");
Sb. append (name + ",");
Sb. append (phoneNumber + ",");
Sb. append (sms );
Sb. append ("}");
If (null = sms)
Sms = "";
} While (cur. moveToNext ());
}
Else
{
Sb. append ("no result! ");
}
Return sb;
}
}
Remember to add the permission android. permission. READ_SMS to AndroidManifest. xml.
<Uses-permission android: name = "android. permission. READ_SMS"/>
Author "ye of a person"