Changes

Jump to: navigation, search
no edit summary
Launch Android Studio and create a new project, entering SendBroadcast into the Application name field and ebookfrenzy.com as the Company Domain setting before clicking on the Next button.
On the form factors screen, enable the Phone and Tablet option and set the minimum SDK setting to API 8: Android 2.2 (Froyo). Continue to proceed through the screens, requesting the creation of a blank activity named SendBroadcastActivity with a corresponding layout and menu resource files named activity_send_broadcastand menu_send_broadcast respectively.
Once the new project has been created, locate and load the activity_send_broadcast.xml layout file located in the Project tool window under SendBroadcast -> app -> src -> main -> res -> layout and, with the Designer tool in Design mode, replace the TextView object with a Button view. Move the button to the center of the display, double click on it and set the text property so that it reads “Send Broadcast”. Once the text value has been set, use the light bulb menu icon to extract the string to a resource named button_text.
With the button still selected in the layout, locate the onClick property in the Properties panel and configure it to call a method named broadcastIntent. A useful shortcut for finding properties is to click on the toolbar of the Properties panel and start typing the first few characters of the property name. This will trigger the search system and begin selecting the best matches for the property name.
 
== Creating and Sending the Broadcast Intent ==
In order to create the broadcast receiver, a new class needs to be created which subclasses the BroadcastReceiver superclass. Create a new project with the application name set to BroadcastReceiver and the company domain name set to com.ebookfrenzy, this time selecting the Add No Activity option before clicking on Finish.
Within the Project tool window, navigate to BroadcastReceiver -> app -> src -> main -> java and right click on the package name. From the resulting menu, select the New -> Other -> Broadcast Receiver menu option, name the class MyReceiver and make sure the Exported and Enabled options are selected.
Once created, Android Studio will automatically load the new MyReceiver.java class file into the editor where it should read as follows:
== Testing the Broadcast Example ==
In order to test the broadcast sender and receiver, begin by running the BroadcastReceiver application on a physical Android deviceor AVD. When the Edit configuration dialog appears, select the Do not launch Activity option so that the activity is installed but not launched and click on the Run button. The successful installation of the package will be reported in the Android Studio Run tool window:
<pre>

Navigation menu