Android reads, writes, and deletes Short Messages

Source: Internet
Author: User

Android receives and sends text messages
1. Android sends text messages
The android API provides the smsmanager class for text message processing. The sendtextmessage (Num, null, content, pend, null) function is to send
Text message method. The first parameter is the target mobile phone number, the second parameter is the SMS center address, null is the default address,
The text content of the third parameter text message, and the fourth parameter is an intent that will bring back the sending result. The fifth parameter is unknown. It is generally null.
To send text messages to an application, you must add the Android. Permission. send_sms permission to androidmanifest. xml.

If you send Chinese characters in the simulator, garbled characters will occur on the receiver, but no garbled characters will occur on the real machine. So
Developers only need to normally develop the text message function without coding and conversion.

It is also convenient to receive text messages, mainly inheriting the broadcasereceiver class, covering the onreceive function:
1: related classes:
Android. content. broadcastreceiver
Android. telephony. GSM. smsmessage;

2: Example code.

Public class messagedemo extends broadcastreceiver {
Private Static final string stract = "android. provider. telephony. sms_received ";
Public void onreceive (context, intent ){
If (intent. getaction (). Equals (stract )){
Stringbuilder sb = new stringbuilder ();
Bundle bundle = intent. getextras ();
If (bundle! = NULL ){
Object [] PDUS = (object []) bundle. Get ("PDUS ");
Smsmessage [] MSG = new smsmessage [PDUS. Length];
For (INT I = 0; I <PDUS. length; I ++ ){
MSG [I] = smsmessage. createfrompdu (byte []) PDUS [I]);
}
For (smsmessage currmsg: MSG ){
SB. append ("from :");
SB. append (currmsg. getdisplayoriginatingaddress ());
SB. append ("\ nmessage :");
SB. append (currmsg. getdisplaymessagebody ());
}
}
}

}
3: Related configurations

Modify androidmanifest. xml and add the worker er node under the activity:
<Cycler Android: Name = "messagedemo">
<Intent-filter>
<Action Android: Name = "android. provider. telephony. sms_received"/>
</Intent-filter>
</Cycler>
Then add nodes under application:
<Uses-Permission Android: Name = "android. Permission. send_sms"> </uses-Permission>
<Uses-Permission Android: Name = "android. Permission. receive_sms"> </uses-Permission>

4: disadvantages of using broadcasted er
View the broadcaster SDK reference. You can see that all broadcaster messages are received in an unordered state. Even if ordered broadcasts is used for broadcaster with the same priority, unordered messages are generated.
So next we will introduce another behavior of Receiving text messages, including deleting text messages.

5. Monitor the sending and receiving of SMS from the database
// The following is mainly used for internal database changes and external interface (activity) for response.
Class smshandler extends Handler
{
Public void handlemessage (Message MSG)
{
// Handle message
}
}
// After receiving the short message, the handlemessage is deleted and does not respond to the interface. Therefore, the handlemessage above is empty.
Class smsobserver extends contentobserver
{
Private handler m_handle = NULL;

Public smsobserver (handler handle)
{
Super (handle );
M_handle = handle;
}
Public void onchange (Boolean bselfchange)
{
Super. onchange (bselfchange );

// Send message to activity
Message MSG = new message ();
MSG. OBJ = "xxxxxxxxxx ";
M_handle.sendmessage (MSG );

String struriinbox = "content: // SMS/inbox ";
Uri urisms = URI. parse (struriinbox); // if you want to access all SMS, just replace the URI string to "content: // SMS /"
Cursor c = mcontext. getcontentresolver (). Query (urisms, null );
// Delete all SMS here when every new SMS occures.
While (C. movetonext ())
{
// Read the contents of the SMS;
For (int I; I <C. getcolumncount (); I ++)
{
String strcolumnname = C. getcolumnname (I );
String strcolumnvalue = C. getstring (I );
}
// Delete the SMS
String pid = C. getstring (1); // get thread ID;
String uri = "content: // SMS/conversations/" + PID;
Mcontext. getcontentresolver (). Delete (URI. parse (URI), null, null );

}
}
}

// Apply the basic class to the Function
Contentresolver = getcontentresolver (); // getcontentresolver () in the context ()
Handler handler = new smshandler ();
Contentobserver m_smsobserver = new smsobserver (handler );
Contentresolver. registercontentobserver (URI. parse ("content: // SMS/inbox"), true, m_smsobserver );
// Register to observe SMS in outbox, we can observe SMS in other location by changing URI string, such as Inbox, sent, draft, Outbox, etc .)

// Some available URI string for SMS.
/*
String struriinbox = "content: // SMS/inbox"; // sms_inbox: 1
String strurifailed = "content: // SMS/failed"; // sms_failed: 2
String struriqueued = "content: // SMS/queued"; // sms_queued: 3
String strurisent = "content: // SMS/sent"; // sms_sent: 4
String struridraft = "content: // SMS/Draft"; // sms_draft: 5
String strurioutbox = "content: // SMS/outbox"; // sms_outbox: 6
String struriundelivered = "content: // SMS/undelivered"; // sms_undelivered
String struriall = "content: // SMS/all"; // sms_all
String struriconversations = "content: // SMS/conversations"; // you can delete one conversation by thread_id
String struriall = "content: // SMS" // you can delete one message by _ id
*/
Remeber: must request following permission
1) read SMS
<Uses-permssion Android: Name = "android. Permission. read_sms"/>
2) delete/modify/send SMS
<Uses-permssion Android: Name = "android. Permission. write_sms"/>
In androidmanifest. xml

Related Article

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.