The text message files read in Android include
[Java]
/**
* All text messages
*/
Public static final String SMS_URI_ALL = "content: // sms /";
/**
* Inbox text message
*/
Public static final String SMS_URI_INBOX = "content: // sms/inbox ";
/**
* Send a text message to the sender
*/
Public static final String SMS_URI_SEND = "content: // sms/sent ";
/**
* Draft box text message
*/
Public static final String SMS_URI_DRAFT = "content: // sms/draft ";
The SMS messages read include:
_ Id: SMS serial number, such as 100
* Thread_id: the serial number of the dialog, for example, 100. It is the same as the serial number of the text message sent by the same mobile phone number.
* Address: the sender address, that is, the mobile phone number, for example, + 8613811810000.
* Person: sender. If the sender is a specific name in the address book, the stranger is null.
* Date: date, long type, such as 1256539465022. You can set the date display format.
* Protocol: protocol 0SMS_RPOTO SMS, 1MMS_PROTO MMS read: whether to read 0 unread, 1 read
* Status: SMS status-1 received, 0 complete, 64 pending, 128 failed
* Type: SMS type 1 is received, and 2 is sent body: the specific content of the SMS.
* Service_center: Number of the SMS service center, for example, + 8613800755500
Next we can create an SmsInfo to extract the following information:
[Java]
Package com.pei.info;
/**
* Class name: SmsInfo <BR>
* Class description: class for retrieving various text message information <BR>
* PS: <BR>
* Date: 2012-3-19 <BR>
*
* @ Version 1.00
* @ Author CODYY) peijiangping
*/
Public class SmsInfo {
/**
* Text message content
*/
Private String smsbody;
/**
* Phone number for sending the text message
*/
Private String phoneNumber;
/**
* Date and time when the SMS is sent
*/
Private String date;
/**
* Name of the sender
*/
Private String name;
/**
* SMS type 1 is received, and 2 is sent
*/
Private String type;
Public String getSmsbody (){
Return smsbody;
}
Public void setSmsbody (String smsbody ){
This. smsbody = smsbody;
}
Public String getPhoneNumber (){
Return phoneNumber;
}
Public void setPhoneNumber (String phoneNumber ){
This. phoneNumber = phoneNumber;
}
Public String getDate (){
Return date;
}
Public void setDate (String date ){
This. date = date;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public String getType (){
Return type;
}
Public void setType (String type ){
This. type = type;
}
}
Then the encapsulation class reads the Information Content SmsContent. java
[Java]
Package com. pei. util;
Import java. util. ArrayList;
Import java. util. List;
Import com.pei.info. SmsInfo;
Import android. app. Activity;
Import android. database. Cursor;
Import android.net. Uri;
/**
* Class name: SmsChoose <BR>
* Class description: obtains various text message information on the mobile phone. <BR>
* PS: permission required <uses-permission android: name = "android. permission. READ_SMS"/> <BR>
* Date: 2012-3-19 <BR>
*
* @ Version 1.00
* @ Author CODYY) peijiangping
*/
Public class SmsContent {
Private Activity; // There is an activity object here. I don't know why I didn't seem to need it before. Now I need it. Try it by yourself.
Private Uri uri;
List <SmsInfo> infos;
Public SmsContent (Activity activity, Uri uri ){
Infos = new ArrayList <SmsInfo> ();
This. activity = activity;
This. uri = uri;
}
/**
* Role: get various text message information. <BR>
* Date: 2012-3-19 <BR>
*
* @ Author CODYY) peijiangping
*/
Public List <SmsInfo> getSmsInfo (){
String [] projection = new String [] {"_ id", "address", "person ",
"Body", "date", "type "};
Cursor cusor = activity. managedQuery (uri, projection, null, null,
"Date desc ");
Int nameColumn = cusor. getColumnIndex ("person ");
Int phoneNumberColumn = cusor. getColumnIndex ("address ");
Int smsbodyColumn = cusor. getColumnIndex ("body ");
Int dateColumn = cusor. getColumnIndex ("date ");
Int typeColumn = cusor. getColumnIndex ("type ");
If (cusor! = Null ){
While (cusor. moveToNext ()){
SmsInfo smsinfo = new SmsInfo ();
Smsinfo. setName (cusor. getString (nameColumn ));
Smsinfo. setDate (cusor. getString (dateColumn ));
Smsinfo. setPhoneNumber (cusor. getString (phoneNumberColumn ));
Smsinfo. setSmsbody (cusor. getString (smsbodyColumn ));
Smsinfo. setType (cusor. getString (typeColumn ));
Infos. add (smsinfo );
}
Cusor. close ();
}
Return infos;
}
}
Provide a listview to display the text message content:
[Java]
Package com. pei. activity;
Import java. util. List;
Import com. pei. fixed. AllFinalInfo;
Import com.pei.info. SmsInfo;
Import com. pei. util. SmsContent;
Import android. app. Activity;
Import android. content. Context;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. ListView;
Import android. widget. TextView;
/**
* Class name: SmsListActivity <BR>
* Class description: displays the SMS list. <BR>
* PS: <BR>
* Date: 2012-3-19 <BR>
*
* @ Version 1.00
* @ Author CODYY) peijiangping
*/
Public class SmsListActivity extends Activity {
Private ListView listview;
Private List <SmsInfo> infos;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. sms );
Uri uri = Uri. parse (AllFinalInfo. SMS_URI_INBOX );
SmsContent SC = new SmsContent (this, uri );
Infos = SC. getSmsInfo ();
Listview = (ListView) this. findViewById (R. id. ListView_Sms );
Listview. setAdapter (new SmsListAdapter (this ));
}
Class SmsListAdapter extends BaseAdapter {
Private LayoutInflater layoutinflater;
Private View myview;
Public SmsListAdapter (Context c ){
Layoutinflater = LayoutInflater. from (c );
}
@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return infos. size ();
}
@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return null;
}
@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return 0;
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
If (convertView = null ){
Myview = layoutinflater. inflate (R. layout. smsitem, null );
}
TextView body = (TextView) myview
. FindViewById (R. id. TextView_SmsBody );
TextView name = (TextView) myview
. FindViewById (R. id. TextView_SmsName );
Body. setText (infos. get (position). getSmsbody ());
Name. setText (infos. get (position). getName ());
Return myview;
}
}
}
In this way, you can read various text message contents at will, and display is very convenient. Methods can be further encapsulated. To be improved.
Android learning and sharing from zookeeper