Android - Broadcast Receivers

<blockquote > <p ><b >Broadcast Receivers</b>simply respond to broadcast messages from other applications or from the system itself. These messages are sometime called events or intents. For example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is broadcast receiver who will intercept this communication and will initiate appropriate action.</p> </blockquote> <p >There are following two important steps to make BroadcastReceiver works for the system broadcasted intents &minus;</p> <ul class="list" > <li > <p >Creating the Broadcast Receiver.</p> </li> <li > <p >Registering Broadcast Receiver</p> </li> </ul> <p >There is one additional steps in case you are going to implement your custom intents then you will have to create and broadcast those intents.</p> <h2 >Creating the Broadcast Receiver</h2> <p >A broadcast receiver is implemented as a subclass of<b >BroadcastReceiver</b>class and overriding the onReceive() method where each message is received as a<b >Intent</b>object parameter.</p> <pre class="prettyprint notranslte prettyprinted" > <span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >MyReceiver</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >BroadcastReceiver</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onReceive</span><span class="pun" >(</span><span class="typ" >Context</span><span class="pln" > context</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >Intent</span><span class="pln" > intent</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >makeText</span><span class="pun" >(</span><span class="pln" >context</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >&quot;Intent Detected.&quot;</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >LENGTH_LONG</span><span class="pun" >).</span><span class="pln" >show</span><span class="pun" >();</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >}</span></pre> <h2 >Registering Broadcast Receiver</h2> <p >An application listens for specific broadcast intents by registering a broadcast receiver in<i >AndroidManifest.xml</i>file. Consider we are going to register<i >MyReceiver</i>for system generated event ACTION_BOOT_COMPLETED which is fired by the system once the Android system has completed the boot process.</p> <p><img alt="broadcast" src="http://www.tutorialspoint.com/android/images/broadcast.jpg" /></p> <h4 align="center" >BROADCAST-RECEIVER</h4> <pre class="prettyprint notranslte prettyprinted" > <span class="tag" >&lt;application</span><span class="pln" > </span><span class="atn" >android:icon</span><span class="pun" >=</span><span class="atv" >&quot;@drawable/ic_launcher&quot;</span><span class="pln" > </span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >&quot;@string/app_name&quot;</span><span class="pln" > </span><span class="atn" >android:theme</span><span class="pun" >=</span><span class="atv" >&quot;@style/AppTheme&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;receiver</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;MyReceiver&quot;</span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;action</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;android.intent.action.BOOT_COMPLETED&quot;</span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;/action&gt;</span><span class="pln" > </span><span class="tag" >&lt;/intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;/receiver&gt;</span><span class="pln" > </span><span class="tag" >&lt;/application&gt;</span></pre> <p >Now whenever your Android device gets booted, it will be intercepted by BroadcastReceiver<i >MyReceiver</i>and implemented logic inside<i >onReceive()</i>will be executed.</p> <p >There are several system generated events defined as final static fields in the<b >Intent</b>class. The following table lists a few important system events.</p> <table class="table table-bordered" > <tbody > <tr > <th >Event Constant</th> <th >Description</th> </tr> <tr > <td >android.intent.action.BATTERY_CHANGED</td> <td >Sticky broadcast containing the charging state, level, and other information about the battery.</td> </tr> <tr > <td >android.intent.action.BATTERY_LOW</td> <td >Indicates low battery condition on the device.</td> </tr> <tr > <td >android.intent.action.BATTERY_OKAY</td> <td >Indicates the battery is now okay after being low.</td> </tr> <tr > <td >android.intent.action.BOOT_COMPLETED</td> <td >This is broadcast once, after the system has finished booting.</td> </tr> <tr > <td >android.intent.action.BUG_REPORT</td> <td >Show activity for reporting a bug.</td> </tr> <tr > <td >android.intent.action.CALL</td> <td >Perform a call to someone specified by the data.</td> </tr> <tr > <td >android.intent.action.CALL_BUTTON</td> <td >The user pressed the &quot;call&quot; button to go to the dialer or other appropriate UI for placing a call.</td> </tr> <tr > <td >android.intent.action.DATE_CHANGED</td> <td >The date has changed.</td> </tr> <tr > <td >android.intent.action.REBOOT</td> <td >Have the device reboot.</td> </tr> </tbody> </table> <h2 >Broadcasting Custom Intents</h2> <p >If you want your application itself should generate and send custom intents then you will have to create and send those intents by using the<i >sendBroadcast()</i>method inside your activity class. If you use the<i >sendStickyBroadcast(Intent)</i>method, the Intent is<b >sticky</b>, meaning the<i >Intent</i>you are sending stays around after the broadcast is complete.</p> <pre class="prettyprint notranslte prettyprinted" > <span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > broadcastIntent</span><span class="pun" >(</span><span class="typ" >View</span><span class="pln" > view</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="typ" >Intent</span><span class="pln" > intent </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >Intent</span><span class="pun" >();</span><span class="pln" > intent</span><span class="pun" >.</span><span class="pln" >setAction</span><span class="pun" >(</span><span class="str" >&quot;com.tutorialspoint.CUSTOM_INTENT&quot;</span><span class="pun" >);</span><span class="pln" > sendBroadcast</span><span class="pun" >(</span><span class="pln" >intent</span><span class="pun" >);</span><span class="pln" > </span><span class="pun" >}</span></pre> <p >This intent<i >com.tutorialspoint.CUSTOM_INTENT</i>can also be registered in similar way as we have regsitered system generated intent.</p> <pre class="prettyprint notranslte prettyprinted" > <span class="tag" >&lt;application</span><span class="pln" > </span><span class="atn" >android:icon</span><span class="pun" >=</span><span class="atv" >&quot;@drawable/ic_launcher&quot;</span><span class="pln" > </span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >&quot;@string/app_name&quot;</span><span class="pln" > </span><span class="atn" >android:theme</span><span class="pun" >=</span><span class="atv" >&quot;@style/AppTheme&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;receiver</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;MyReceiver&quot;</span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;action</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;com.tutorialspoint.CUSTOM_INTENT&quot;</span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;/action&gt;</span><span class="pln" > </span><span class="tag" >&lt;/intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;/receiver&gt;</span><span class="pln" > </span><span class="tag" >&lt;/application&gt;</span></pre> <h2 >Example</h2> <p >This example will explain you how to create<i >BroadcastReceiver</i>to intercept custom intent. Once you are familiar with custom intent, then you can program your application to intercept system generated intents. So let&#39;s follow the following steps to modify the Android application we created in<i >Hello World Example</i>chapter &minus;</p> <table class="table table-bordered" > <tbody > <tr > <th class="fivepct" >Step</th> <th >Description</th> </tr> <tr > <td >1</td> <td >You will use Android studio to create an Android application and name it as<i >My Application</i>under a package<i >com.example.My Application</i>as explained in the<i >Hello World Example</i>chapter.</td> </tr> <tr > <td >2</td> <td >Modify main activity file<i >MainActivity.java</i>to add<i >broadcastIntent()</i>method.</td> </tr> <tr > <td >3</td> <td >Create a new java file called<i >MyReceiver.java</i>under the package<i >com.example.My Application</i>to define a BroadcastReceiver.</td> </tr> <tr > <td >4</td> <td >An application can handle one or more custom and system intents without any restrictions. Every indent you want to intercept must be registered in your<i >AndroidManifest.xml</i>file using &lt;receiver.../&gt; tag</td> </tr> <tr > <td >5</td> <td >Modify the default content of<i >res/layout/activity_main.xml</i>file to include a button to broadcast intent.</td> </tr> <tr > <td >6</td> <td >No need to modify the string file, Android studio take care of string.xml file.</td> </tr> <tr > <td >7</td> <td >Run the application to launch Android emulator and verify the result of the changes done in the application.</td> </tr> </tbody> </table> <p >Following is the content of the modified main activity file<b >src/com.example.My Application/MainActivity.java</b>. This file can include each of the fundamental life cycle methods. We have added<i >broadcastIntent()</i>method to broadcast a custom intent.</p> <pre class="prettyprint notranslte prettyprinted" > <span class="kwd" >package</span><span class="pln" > com</span><span class="pun" >.</span><span class="pln" >example</span><span class="pun" >.</span><span class="typ" >My</span><span class="pln" > </span><span class="typ" >Application</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >os</span><span class="pun" >.</span><span class="typ" >Bundle</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >app</span><span class="pun" >.</span><span class="typ" >Activity</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >Menu</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >Intent</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >View</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >MainActivity</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >Activity</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onCreate</span><span class="pun" >(</span><span class="typ" >Bundle</span><span class="pln" > savedInstanceState</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onCreate</span><span class="pun" >(</span><span class="pln" >savedInstanceState</span><span class="pun" >);</span><span class="pln" > setContentView</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >layout</span><span class="pun" >.</span><span class="pln" >activity_main</span><span class="pun" >);</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onCreateOptionsMenu</span><span class="pun" >(</span><span class="typ" >Menu</span><span class="pln" > menu</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > getMenuInflater</span><span class="pun" >().</span><span class="pln" >inflate</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >menu</span><span class="pun" >.</span><span class="pln" >activity_main</span><span class="pun" >,</span><span class="pln" > menu</span><span class="pun" >);</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="com" >// broadcast a custom intent. </span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > broadcastIntent</span><span class="pun" >(</span><span class="typ" >View</span><span class="pln" > view</span><span class="pun" >){</span><span class="pln" > </span><span class="typ" >Intent</span><span class="pln" > intent </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >Intent</span><span class="pun" >();</span><span class="pln" > intent</span><span class="pun" >.</span><span class="pln" >setAction</span><span class="pun" >(</span><span class="str" >&quot;com.tutorialspoint.CUSTOM_INTENT&quot;</span><span class="pun" >);</span><span class="pln" > sendBroadcast</span><span class="pun" >(</span><span class="pln" >intent</span><span class="pun" >);</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >}</span></pre> <p >Following is the content of<b >src/com.example.My Application/MyReceiver.java</b>:</p> <pre class="prettyprint notranslte prettyprinted" > <span class="kwd" >package</span><span class="pln" > com</span><span class="pun" >.</span><span class="pln" >example</span><span class="pun" >.</span><span class="typ" >My</span><span class="pln" > </span><span class="typ" >Application</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >BroadcastReceiver</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >Context</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >Intent</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >Toast</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >MyReceiver</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >BroadcastReceiver</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onReceive</span><span class="pun" >(</span><span class="typ" >Context</span><span class="pln" > context</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >Intent</span><span class="pln" > intent</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >makeText</span><span class="pun" >(</span><span class="pln" >context</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >&quot;Intent Detected.&quot;</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >LENGTH_LONG</span><span class="pun" >).</span><span class="pln" >show</span><span class="pun" >();</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >}</span></pre> <p >Following will the modified content of<i >AndroidManifest.xml</i>file. Here we have added &lt;service.../&gt; tag to include our service:</p> <pre class="prettyprint notranslte prettyprinted" > <span class="tag" >&lt;manifest</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/apk/res/android&quot;</span><span class="pln" > </span><span class="atn" >package</span><span class="pun" >=</span><span class="atv" >&quot;com.example.My Application&quot;</span><span class="pln" > </span><span class="atn" >android:versionCode</span><span class="pun" >=</span><span class="atv" >&quot;1&quot;</span><span class="pln" > </span><span class="atn" >android:versionName</span><span class="pun" >=</span><span class="atv" >&quot;1.0&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;uses-sdk</span><span class="pln" > </span><span class="atn" >android:minSdkVersion</span><span class="pun" >=</span><span class="atv" >&quot;8&quot;</span><span class="pln" > </span><span class="atn" >android:targetSdkVersion</span><span class="pun" >=</span><span class="atv" >&quot;22&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;application</span><span class="pln" > </span><span class="atn" >android:icon</span><span class="pun" >=</span><span class="atv" >&quot;@drawable/ic_launcher&quot;</span><span class="pln" > </span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >&quot;@string/app_name&quot;</span><span class="pln" > </span><span class="atn" >android:theme</span><span class="pun" >=</span><span class="atv" >&quot;@style/AppTheme&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;activity</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;.MainActivity&quot;</span><span class="pln" > </span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >&quot;@string/title_activity_main&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;action</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;android.intent.action.MAIN&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;category</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;android.intent.category.LAUNCHER&quot;</span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;/intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;/activity&gt;</span><span class="pln" > </span><span class="tag" >&lt;receiver</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;MyReceiver&quot;</span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;action</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;com.tutorialspoint.CUSTOM_INTENT&quot;</span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;/action&gt;</span><span class="pln" > </span><span class="tag" >&lt;/intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;/receiver&gt;</span><span class="pln" > </span><span class="tag" >&lt;/application&gt;</span><span class="pln" > </span><span class="tag" >&lt;/manifest&gt;</span></pre> <p >Following will be the content of<b >res/layout/activity_main.xml</b>file to include a button to broadcast our custom intent &minus;</p> <pre class="prettyprint notranslte prettyprinted" > <span class="tag" >&lt;RelativeLayout</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/apk/res/android&quot;</span><span class="pln" > </span><span class="atn" >xmlns:tools</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/tools&quot;</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;match_parent&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;match_parent&quot;</span><span class="pln" > </span><span class="atn" >android:paddingLeft</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_horizontal_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingRight</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_horizontal_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingTop</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_vertical_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingBottom</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_vertical_margin&quot;</span><span class="pln" > </span><span class="atn" >tools:context</span><span class="pun" >=</span><span class="atv" >&quot;.MainActivity&quot;</span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;TextView</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView1&quot;</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Example of Broadcast&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignParentTop</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >&quot;30dp&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;TextView</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView2&quot;</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Tutorials point &quot;</span><span class="pln" > </span><span class="atn" >android:textColor</span><span class="pun" >=</span><span class="atv" >&quot;#ff87ff09&quot;</span><span class="pln" > </span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >&quot;30dp&quot;</span><span class="pln" > </span><span class="atn" >android:layout_above</span><span class="pun" >=</span><span class="atv" >&quot;@+id/imageButton&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:layout_marginBottom</span><span class="pun" >=</span><span class="atv" >&quot;40dp&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;ImageButton</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/imageButton&quot;</span><span class="pln" > </span><span class="atn" >android:src</span><span class="pun" >=</span><span class="atv" >&quot;@drawable/abc&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerVertical</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;Button</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/button2&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Broadcast Intent&quot;</span><span class="pln" > </span><span class="atn" >android:onClick</span><span class="pun" >=</span><span class="atv" >&quot;broadcastIntent&quot;</span><span class="pln" > </span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >&quot;@+id/imageButton&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;/RelativeLayout&gt;</span></pre> <p >Following will be the content of<b >res/values/strings.xml</b>to define two new constants &minus;</p> <pre class="prettyprint notranslte prettyprinted" > <span class="tag" >&lt;resources&gt;</span><span class="pln" > </span><span class="tag" >&lt;string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >&quot;menu_settings&quot;</span><span class="tag" >&gt;</span><span class="pln" >Settings</span><span class="tag" >&lt;/string&gt;</span><span class="pln" > </span><span class="tag" >&lt;string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >&quot;title_activity_main&quot;</span><span class="tag" >&gt;</span><span class="pln" >My Application</span><span class="tag" >&lt;/string&gt;</span><span class="pln" > </span><span class="tag" >&lt;/resources&gt;</span></pre> <p >Let&#39;s try to run our modified<b >Hello World!</b>application we just modified. I assume you had created your<b >AVD</b>while doing environment set-up. To run the app from Android studio, open one of your project&#39;s activity files and click Run<img alt="Eclipse Run Icon" class="inline" src="http://www.tutorialspoint.com/android/images/eclipse_run.jpg" />icon from the tool bar. Android Studio installs the app on your AVD and starts it and if everything is fine with your set-up and application, it will display following Emulator window &minus;</p> <p><img alt="Android Broadcast Demo" src="http://www.tutorialspoint.com/android/images/broadcast1.jpg" /></p> <p >Now to broadcast our custom intent, let&#39;s click on<b >Broadcast Intent</b>button, this will broadcast our custom intent<i >&quot;com.tutorialspoint.CUSTOM_INTENT&quot;</i>which will be intercepted by our registered BroadcastReceiver i.e. MyReceiver and as per our implemented logic a toast will appear on the bottom of the the simulator as follows &minus;</p> <p><img alt="Android Broadcast Intent" src="http://www.tutorialspoint.com/android/images/broadcast2.jpg" /></p> <p >You can try implementing other BroadcastReceiver to intercept system generated intents like system boot up, date changed, low battery etc.</p> <p><!--[/!]*?[^<>]*?>'si", //去掉注释标记<br />                     "'  '",<br />                     );<br />     $replace = array (<br />                     "",<br />                     "",<br />                     );<br />     return preg_replace($pattern, $replace, $string);<br /> }</p> <p>大家可能会说,前面用str_replace,后面怎么又用preg_replace了呢<br /> 其实,我也是迫不得已,能不用正则尽量不用,但是要删除注释和多余的空格,必须要用正则</p> <p>这个函数也有局限性<br /> 如果html代码中出现以下情况,可能会出现异常<br /> 1.标签中出现2个或以上的空格,大家知道规范的写法是:<a_href=''>,但是有人就会写成:<a__href=''>,其中_表示空格,这种情况会导致异常错误<br /> 2.除了标签以外存在一个空格,那么无法清除<br /> 3.可能正文中存在必要的空格也被清除<br /> 4.代码中如果存在javascript,并且有//存在,那么由于没有换行了,所以后面的js代码都被注释掉了,导致js出错或者不起作用<br /> 5.其他未知异常</p>--></p>
RangeTime:0.006705s
RangeMem:239.55 KB
返回顶部 留言