Using intent to send broadcast messages, the content of the broadcast message can be either application-related data information or Android system messages such as network connection changes, battery change, receiving text messages, or system settings changes.
:
Code: (main activity)
1 PackageCom.example.playmessage;2 3 Importandroid.app.Activity;4 Importandroid.content.Intent;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.Menu;7 ImportAndroid.view.MenuItem;8 ImportAndroid.view.View;9 ImportAndroid.view.View.OnClickListener;Ten ImportAndroid.widget.Button; One A Public classMainactivityextendsActivity { - Button Ob1; - Intent Intent; the @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); + -ob1=(Button) Findviewbyid (R.ID.BU1); Button +Intent=NewIntent ("Com.example.playmessage"); Set unique identities AOb1.setonclicklistener (NewOnclicklistener () { at - @Override - Public voidOnClick (View v) { - //TODO auto-generated Method Stub - Sendbroadcast (Intent); Send broadcast - in } - }); to + - } the * $}
Broadcast-receiver
1 PackageCom.example.playmessage;2 3 ImportAndroid.content.BroadcastReceiver;4 ImportAndroid.content.Context;5 Importandroid.content.Intent;6 ImportAndroid.media.MediaPlayer;7 ImportAndroid.widget.Toast;8 9 Public classMyreceiverextendsBroadcastreceiver {Ten PublicMyreceiver () { One } A MediaPlayer play; - @Override - Public voidOnReceive (Context context, Intent Intent) { the //Todo:this method is called if the Broadcastreceiver is receiving - //An Intent broadcast. -Toast.maketext (Context, "receive broadcast, play Music", 2). Show ();; -play=mediaplayer.create (context,r.raw.sleep); + Play.start (); - + } A}
Need to add intent filtering in the manifest file under Broadcast-receiver
<intent-filter>
<action android:name= "Unique identification of settings"/>
<intent-filter>
Android broadcast Receiver (broadcast message) application example