SMS processing for Android-use of broadcast and Receiver

Source: Internet
Author: User

<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. mysrc "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name"> <activity Android: Name = ". activitymain1 "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. CA Tegory. launcher "/> </intent-filter> </activity> <activity Android: Name =" secondactivity "/> <Cycler Android: Name = ". mybroadcastreceiver "> <intent-filter> <! -- <Action Android: Name = "android. intent. action. boot_completed "/> boot Android system --> <action Android: Name =" android. provider. telephony. sms_received "/> </intent-filter> </Cycler> </Application> <! -- <Uses-Permission Android: Name = "android. permission. receive_boot_completed "/> get the system startup permission --> <uses-Permission Android: Name =" android. permission. receive_sms "> </uses-Permission> <! -- SMS receiving permission of the program --> <uses-Permission Android: Name = "android. Permission. read_sms"> </uses-Permission> <! -- Read SMS permission --> </manifest>

The Android system broadcasts a specific broadcast to the whole system during certain actions or status changes. If a package contains a receiver that matches the broadcast, you can use this broadcast to trigger custom processing.

The following example describes how to use broadcast and receiver.

The receiver class code is as follows:

Import ............; public class mybroadcastreceiver extends broadcastreceiver {// static final string action = "android. intent. action. boot_completed "; static final string action =" android. provider. telephony. sms_received "; broadcast ID @ override public void onreceive (context, intent) {If (intent. getaction (). equals (Action) {// start the service bundle = intent. ge Textras (); object messages [] = (object []) bundle. get ("PDUS"); smsmessage [] = new smsmessage [messages. length]; for (INT n = 0; n <messages. length; n ++) {// multiple messages may be received at a time ???? Smsmessage [N] = smsmessage. createfrompdu (byte []) messages [N]); string sender = smsmessage [N]. getoriginatingaddress (); // message sender string content = smsmsmessage [N]. getmessagebody (); // get the text message content date = new date (smsmessage [N]. gettimestampmillis (); // obtain the message receipt time simpledateformat = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); string sendtime = format. format (date); // toast. maketext (context, "Sender:" + sender + "\ r \ n # Sending time:" + sendtime + // "\ r \ n # Content:" + content, toast. length_long ). show (); intent myintent = new intent (context, secondactivity. class); myintent. setflags (intent. flag_activity_new_task); // you must set intent
// Flag_activity_new_task flag. Otherwise, an error is returned.
Context. startactivity (myintent); // smsmanager manager = smsmanager. getdefault (); // manager. sendtextmessage ("5556", null, "Sender:" + sender // + "----- sending time:" + sendtime + "---- content:" + content, null, null ); // send the intercepted text message to your specified mobile phone. The value is 5556 }}}}

Android program of various permissions and instructions to participate in the address: http://yangguangfu.javaeye.com/blog/753388

Various broadcast logos for Android systems see address: http://hi.baidu.com/pengwenfu/blog/item/3e1f5d2ba7dc0723d52af118.html

The androidmanifest. XML Code is as follows (register the handler and grant permissions to the program ):


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.