Android Concise Development tutorial 20: Broadcast Receiver SMS Trigger sample

Source: Internet
Author: User
Tags filter

Broadcast receiver in Android can be used to listen for broadcast events. Before using broadcast, you must either use code or register in Androidmanifest.xml.

The following example implements using SMS to trigger an example in androidgraphics2dtutorial. SMS format: @demo: xxxx, xxxx For example name, for example, launch Colors example, send to mobile phone: @demo: Colors. Mobile phone after receiving a message, first detect the format of the text message to meet @demo: xxxx, if it is met, this starts the corresponding example.

Add a custom broadcast Receiver smsmessagereceiver to the androidgraphics2dtutorial to monitor received SMS:

public class Smsmessagereceiver extends Broadcastreceiver {
private static final String querystring= "@demo:";
@Override
public void OnReceive (context context, Intent Intent) {
Bundle extras = Intent.getextras ();
if (extras = null)
Return
Object[] PDUs = (object[]) extras.get ("PDUs");
for (int i = 0; i < pdus.length; i++) {
Smsmessage message = SMSMESSAGE.CREATEFROMPDU ((byte[)) pdus[i]);
String fromaddress = message.getoriginatingaddress ();
String fromdisplayname = fromaddress;
String Msg=message.getmessagebody ();
if (Msg.startswith (querystring)) {
Trigger The main activity to fire up a dialog
That shows/reads the received messages
Intent di = new Intent ();
Di.setclass (context, androidgraphics2dtutorial.class);
Di.addflags (Intent.flag_activity_new_task
| Intent.flag_activity_single_top);
Di.putextra (Androidgraphics2dtutorial.sms_from_address_extra,
fromaddress);
Di.putextra (Androidgraphics2dtutorial.sms_from_display_name_extra,
Fromdisplayname);
Di.putextra (Androidgraphics2dtutorial.sms_message_extra, msg);
Context.startactivity (DI);
}
}
}
}

OnReceive will be executed in the broadcast event, where the SMS content is detected, and if the @demo: begins, then the androidgraphics2dtutorial Main activity is initiated.

Modify Androidmainifest.xml

<receiver android:name= ". Smsmessagereceiver "android:enabled=" True >
<intent-filter>
<action android:name= "Android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>

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.