<span id="Label3"></p><p><p>There are two ways for Android to broadcast</p></p><p><p>Mode 1: Dynamic Registration</p></p><p><p>Mode 2: Profile Registration</p></p><p><p></p></p><p><p>Let's implement mode 1, dynamic registration</p></p><p><p>First we define a recipient, inherit <span style="color: #000000;">broadcastreceiver </span>, and override the <span style="color: #000000;">onreceive</span> method, which automatically calls the <span style="color: #000000;">OnReceive</span> method when the specified broadcast is Emitted.</p></p><pre><pre><span style="color: #0000ff;"></span> public <span style="color: #0000ff;">class</span> <span style="color: #0000ff;">extends</span> <span style="color: #000000;">broadcastreceiver { @Override </span>public<span style="color: #0000ff;"></span><span style="color: #0000ff;">void</span> <span style="color: #000000;"> onreceive (context context, Intent Intent) { </span><span style="color: #008000;">//</span> <span style="color: #008000;">Get the data passed in the broadcast as a</span> String, intent.getstringextra ("data"<span style="color: #000000;">); LOG.I (</span>"shxtapp", "received data:" + data<span style="color: #000000;">);} }</span></pre></pre><p><p>Next we register this broadcast receiver, here we specify the broadcast action is "setaction", only the system issued this broadcast, can be received by us.</p></p><pre><pre><span style="color: #0000ff;">New</span><span style="color: #000000;"></span><span style="color: #0000ff;">new</span> <span style="color: #000000;"> intentfilter (); filter.addaction (</span><span style="color: #008000;">//</span> <span style="color: #008000;">only the recipient who holds the same action can receive this broadcast </span> <span style="color: #000000;">registerreceiver (receivebroadcast, filter); LOG.I (</span>"shxtapp", "registered recipient complete");</pre></pre><p><p></p></p><p><p>Next we simulate the broadcast</p></p><pre><pre><span style="color: #0000ff;">New</span> Intent (); <span style="color: #008000;">//</span> <span style="color: #008000;">Itent is the content we want to send</span> Intent.putextra ("data", "transmitted information"<span style="color: #000000;">), intent.setaction (</span>"setaction"<span style="color: #000000;">);</span> <span style="color: #008000;">//</span> <span style="color: #008000;">set your action on this broadcast, only</span> the recipient who is the same as this action can receive the broadcast Sendbroadcast (intent); <span style="color: #008000;">//</span> <span style="color: #008000;">Send broadcast</span> LOG.I ("shxtapp", "send complete");</pre></pre><p><p>When we broadcast, our defined broadcast receivers receive our broadcasts and are able to obtain the data we transmit.</p></p>
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.