One, the code
1.xml
(1) Main.xml
1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3android:orientation= "Vertical"4Android:layout_width= "Fill_parent"5android:layout_height= "Fill_parent"6>7<Button8Android:id= "@+id/register"9Android:layout_width= "Fill_parent"Tenandroid:layout_height= "Wrap_content" Oneandroid:text= "Binding Listener" A/> -<Button -Android:id= "@+id/unregister" theAndroid:layout_width= "Fill_parent" -android:layout_height= "Wrap_content" -android:text= "Unblocking listener bindings" -/> + -</LinearLayout>
(2) AndroidManifest.xml.xml
1<?xml version= "1.0" encoding= "Utf-8"?>2<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"3 Package= "Com.broadcast2"4Android:versioncode= "1"5Android:versionname= "1.0" >6 7<uses-SDK8Android:minsdkversion= "8"9Android:targetsdkversion= "/>"Ten One<Application AAndroid:allowbackup= "true" -android:icon= "@drawable/ic_launcher" -Android:label= "@string/app_name" theAndroid:theme= "@style/apptheme" > -<Activity -Android:name= ". Testbc2activity " -Android:label= "@string/app_name" > +<intent-filter> -<action android:name= "Android.intent.action.MAIN"/> + A<category android:name= "Android.intent.category.LAUNCHER"/> at</intent-filter> -</activity> -</application> -<uses-permission android:name= "Android.permission.RECEIVE_SMS"/> -</manifest>
2.java
(1) Testbc2activity.java
1 PackageCom.broadcast2;2 3 Importandroid.app.Activity;4 ImportAndroid.content.IntentFilter;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.View;7 ImportAndroid.view.View.OnClickListener;8 ImportAndroid.widget.Button;9 Ten Public classTestbc2activityextendsActivity { One /**Called when the activity is first created.*/ A PrivateButton Registerbutton =NULL; - PrivateButton Unregisterbutton =NULL; - PrivateSmsreceiver Smsreceiver =NULL; the - Private Static FinalString sms_action = "Android.provider.Telephony.SMS_RECEIVED"; - @Override - Public voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.main); +Registerbutton =(Button) Findviewbyid (r.id.register); ARegisterbutton.setonclicklistener (NewRegisterreceiverlistener ()); atUnregisterbutton =(Button) Findviewbyid (r.id.unregister); -Unregisterbutton.setonclicklistener (NewUnregisterreceiverlistener ()); - } - - classRegisterreceiverlistenerImplementsonclicklistener{ - in @Override - Public voidOnClick (View v) { to //generate a Broiadcastreceiver object +Smsreceiver =Newsmsreceiver (); - //generate a Intentfilter object theIntentfilter filter =NewIntentfilter (); * //add an action to Intentfilter to determine what type of request Reciver can receive $ filter.addaction (sms_action);Panax Notoginseng //registering the Broadcastreceiver object with the system -Testbc2activity. This. Registerreceiver (smsreceiver, filter); the } + A } the + classUnregisterreceiverlistenerImplementsonclicklistener{ - $ @Override $ Public voidOnClick (View v) { - //to unlock the registration of a Broadcastreceiver object -Testbc2activity. This. Unregisterreceiver (smsreceiver); the } - Wuyi } the}
(2) Smsreceiver.java
1 PackageCom.broadcast2;2 3 ImportAndroid.content.BroadcastReceiver;4 ImportAndroid.content.Context;5 Importandroid.content.Intent;6 ImportAndroid.os.Bundle;7 ImportAndroid.telephony.SmsMessage;8 9 Public classSmsreceiverextendsbroadcastreceiver{Ten One @Override A Public voidOnReceive (Context context, Intent Intent) { - //TODO auto-generated Method Stub -SYSTEM.OUT.PRINTLN ("Receive Message"); the - //This example is to get SMS content - //accept data from intent objects -Bundle bundle =Intent.getextras (); + //in the Bundle object, there is a property named PDUs, and the value of this property is an object array -Object[] Myobjpdus = (object[]) bundle.get ("PDUs"); + //Create an array of type Smsmessage Asmsmessage[] Messages =NewSmsmessage[myobjpdus.length]; at System.out.println (messages.length); - for(inti = 0; i<myobjpdus.length; i++) - { - //creating a Smsmessage object using objects in an object array -Messages[i] = SMSMESSAGE.CREATEFROMPDU ((byte[]) myobjpdus[i]); - //Call the Getdisppalymessagebody () method of the Smsmessage object to get the contents of the message in System.out.println (Messages[i].getdisplaymessagebody ()); - } to Try { +Thread.Sleep (30 * 1000); -System.out.println ("-------------------------------"); the}Catch(interruptedexception e) { * //TODO auto-generated Catch block $ e.printstacktrace ();Panax Notoginseng } - } the +}
Android_mars Study Note _s01 original version _013_ broadcasting Mechanism II