Android Radio (broadcast)

Source: Internet
Author: User

The process of broadcasting events
① Register Broadcast event: There are two ways of registering, one is static registration, is defined in the Androidmanifest.xml file, the registered broadcast receiver must inherit Broadcastreceiver, and the other is dynamic registration, is registered in the program using CONTEXT.REGISTERRECEIVER, registered broadcast receiver equivalent to an anonymous class. Both of these methods require intentfilter.
② Send broadcast event: sent by Context.sendbroadcast, by intent to pass the action used to register.
③ Receive broadcast event: When the broadcast being sent is heard by the receiver, its onreceive () method is called, and the intent object containing the message is passed to it. OnReceive the code in the execution time does not exceed 5s, otherwise Android will eject timeout dialog.

Static Registration Broadcast Demo:

Manifest

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.rainwii.client"Android:versioncode= "1"Android:versionname= "1.0"Android:shareduserid= "Com.rainwii.share" >    <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" />    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name= "Com.rainwii.client.MainActivity"Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>        <receiverAndroid:name= "Com.rainwii.client.MyBroadcastReceive"            >            <Intent-filter>                <ActionAndroid:name= "CCC"></Action>             </Intent-filter>        </receiver>    </Application></Manifest>
View Code

Mainactivity

 Packagecom.rainwii.client;ImportCOM.RAINWII.MAIN.R;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.content.Context;Importandroid.content.Intent;Importandroid.content.pm.PackageManager.NameNotFoundException;ImportAndroid.util.Log;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {Button Startappbutton;    String str; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Startappbutton=(Button) Findviewbyid (R.id.button1); Startappbutton.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubIntent Intent =NewIntent (); Intent.putextra ("MSG", "Hello, I am a broadcast"); Intent.setaction ("CCC"); Intent.setclass (mainactivity. This, Mybroadcastreceive.class); Mainactivity. This. Sendbroadcast (Intent);                        }        }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }    }
View Code

Mybroadcastreceive

 Packagecom.rainwii.client;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.widget.Toast; Public classMybroadcastreceiveextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {string string=intent.getstringextra ("MSG"); Toast.maketext (Context,"The broadcast was received!" \ n "+string, Toast.length_short). Show (); }}
View Code

Android Radio (broadcast)

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.