Android listening text message

Source: Internet
Author: User

AndroidYou can useSetWrite a receiver inherited from broadcastreceiver to overwrite and receiveSMSSubsequent logic processing:

< Cycler Android: Name = " . Filter. smsreceiver " >  
< Intent - Filter >
< Action Android: Name = " Android. provider. telephony. sms_received "   />
</ Intent - Filter >
</ Cycler >  

 

 

Broadcast after the SMS is sent:

1 contentobserver

First write an observer:

 

Code

Public   Class Smsobserver extends contentobserver {
Private Context context;
Private   Static Final string [] sms_projection =   New String [] {
" Address " , " Person " , " Date " , " Type " , " Body " ,
};

Public Smsobserver (context, Handler handler ){
Super (handler );
This . Context = Context;
Log. I ( " Leo-smsobserver " , " My oberver on create " );
}

Public   Void Onchange (Boolean selfchange ){
Log. I ( " Smsobserver " , " SMS onchange ###### " );
}

}

 

Then register the observer in acitivty or service.

 

Getcontentresolver (). registercontentobserver (URI. parse ( " Content: // SMS " ),
True , New Smsobserver ( This , New Handler ()));

 

 

2. You can customize broadcastreceiver ~

 

Code

Smsmanager = Smsmanager. getdefault ();

// Todo auto-generated method stub
Try
{
/* Create an intent for the custom action constant (for the pendingintent parameter) */
Intent itsend =   New Intent (sms_send_actioin );
Intent itdeliver =   New Intent (sms_delivered_action );

/* The sentintent parameter is the broadcast information received after transmission pendingintent. */
Pendingintent msendpi = Pendingintent. getbroadcast
(Getapplicationcontext (), 0 , Itsend, 0 );

/* The deliveryintent parameter is the broadcast information received after delivery pendingintent */
Pendingintent mdeliverpi = Pendingintent. getbroadcast
(Getapplicationcontext (), 0 , Itdeliver, 0 );

/* Send SMS messages. Pay attention to the two pendingintent parameters. */
Smsmanager. sendtextmessage
(Strdestaddress, Null , Strmessage, msendpi, mdeliverpi );

Mtextview01.settext (R. String . Str_sms_sending );
}
Catch (Exception E)
{
Mtextview01.settext (E. tostring ());
E. printstacktrace ();
}

Code

/* Custom mservicereceiver overwrite broadcastreceiver to listen to SMS status information */
Public   Class Mservicereceiver extends broadcastreceiver
{
@ Override
Public   Void Onreceive (context, intent)
{
// Todo auto-generated method stub

Try
{
/* Android. content. broadcastreceiver. getresultcode () method */
Switch (Getresultcode ())
{
Case Activity. result_ OK:
/* The text message is sent successfully.Code */
// Mtextview01.settext (R. String. str_sms_sent_success );
Mmaketexttoast
(
Getresources (). gettext
(R. String . Str_sms_sent_success). tostring (),
True
);
Break ;
Case Smsmanager. result_error_generic_failure:
/* Failed to send SMS */
// Mtextview01.settext (R. String. str_sms_sent_failed );
Mmaketexttoast
(
Getresources (). gettext
(R. String . Str_sms_sent_failed). tostring (),
True
);
Break ;
Case Smsmanager. result_error_radio_off:
Break ;
Case Smsmanager. result_error_null_pdu:
Break ;
}
}
Catch (Exception E)
{
Mtextview01.settext (E. tostring ());
E. getstacktrace ();
}
}
}

 

 

 

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.