Mainactivity.java
Package Com.example.broadcasttest;import Android.content.intent;import Android.content.intentfilter;import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class Mainactivity extends ActionBarActivity { Button Btn01; Button btn02; Button btn03; Button Btn04;private Final static String action_sms = "Android.provider.Telephony.SMS_RECEIVED"; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main); btn01 = (Button) Findviewbyid (r.id.btn01); btn02 = (Button) Findviewbyid (R.ID.BTN02); btn03 = (Button) Findviewbyid (r.id.btn03); btn04 = (Button) Findviewbyid (r.id.btn04); Btn01.setonclicklistener (new Mylistenr ()); Btn02.setonclicklistener (New MyListener2 ()); Btn03.setonclicklistener (new Mylistenr ()); Btn04.setonclicklistener ( New Mylistenr ());} Class Mylistenr implements onclicklistener{@Overridepublic void OnClick (ViEW view) {//TODO auto-generated method Stubif (R.id.btn01==view.getid ()) {//Turn on broadcast intent intent = new intent ();// Intent.setaction (Intent.action_edit);//intent.setaction (Intent.action_search); MainActivity.this.sendBroadcast (intent);} else if (R.id.btn03==view.getid ()) {//Turn on broadcast intent intent = new intent (); intent.setaction (action_sms); System.out.println ("Ready to simulate sending SMS"); MainActivity.this.sendBroadcast (intent);} else if (R.id.btn04==view.getid ()) {//Turn on broadcast TestBCReceiver02 receiver = new TestBCReceiver02 (); System.out.println ("= = Cancel monitoring = ="); MainActivity.this.unregisterReceiver (receiver);//This will cause the app to crash, temporarily unaware of the cause}}}class MyListener2 implements onclicklistener{@Overridepublic void OnClick (View arg0) {//TODO auto-generated Method stub//Register broadcast event//1. Get RecevierTestBCReceiver02 receiver = new TestBCReceiver02 ();//2. Generate a Intentfilter object intentfilter filter = new Intentfilter ();//3. Add an ACTION filter.addaction (action_sms) for Intentfilter; MainActivity.this.registerReceiver (receiver, filter);}}}
Recvier Implementation class:
Package Com.example.broadcasttest;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;public class TestBCReceiver02 extends Broadcastreceiver{public TestBCReceiver02 () { System.out.println ("BC02 's constructor begins, ... ");} @Overridepublic void OnReceive (Context arg0, Intent arg1) {System.out.println ("received SMS");}}
Receiver Implementation Class 2
Package Com.example.broadcasttest;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;public class Testbroadcastreceiver extends Broadcastreceiver{public testbroadcastreceiver () { SYSTEM.OUT.PRINTLN ("Receiver started,,,,");} @Overridepublic void OnReceive (Context arg0, Intent arg1) {System.out.println ("OnReceive .....");}}
Android Radio-SMS Blocker