BroadcastReceiver
java.lang.Object
android.content.BroadcastReceiver
BaseclassforcodethatwillreceiveintentssentbysendBroadcast().
YoucaneitherdynamicallyregisteraninstanceofthisclasswithContext.registerReceiver()
orstaticallypublishanimplementationthroughthe<receiver>taginyourAndroidManifest.xml.
Note:IfregisteringareceiverinyourActivity.onResume()implementation,youshouldunregisteritinActivity.onPause().
(Youwon'treceiveintentswhenpaused,andthiswillcutdownonunnecessarysystemoverhead).
DonotunregisterinActivity.onSaveInstanceState(),becausethiswon'tbecallediftheusermovesbackinthehistorystack.
该类主要用来接收sendBroadcast()发出的intent。
可以通过在代码中用Context.registerReceiver()的形式动态注册BroadcastReceiver。
也可以在AndroidManifest.xml中通过<receiver>来注册BroadcastReceiver。
BroadcastReceiver主要有两大类:
Normalbroadcasts( 普通广播 ):It(sentwithContext.sendBroadcast)arecompletelyasynchronous.
Allreceiversofthebroadcastareruninanundefinedorder,oftenatthesametime.
Thisismoreefficient,butmeansthatreceiverscannotusetheresultorabortAPIsincludedhere.
该广播通过 Context.sendBroadcast 这种发送发送。它是完全异步的。
所有的receivers接收器的执行顺序不确定。因此,所有的receivers接收器接收broadcast的顺序不确定。
这种方式效率更高。但是receivers接收器无法使用theresultorabortAPIs
Orderedbroadcasts( 有序广播):It(sentwithContext.sendOrderedBroadcast)aredeliveredtoonereceiveratatime.
Aseachreceiverexecutesinturn,itcanpropagatearesulttothenextreceiver,
oritcancompletelyabortthebroadcastsothatitwon'tbepassedtootherreceivers.
Theorderreceiversrunincanbecontrolledwiththe android:priorityattribute
ofthematchingintent-filter;
receiverswiththesameprioritywillberuninanarbitraryorder.
该广播是通过 Context.sendOrderedBroadcast 来发送。所有的receiver依次执行。
receiver接收器可以把结果传给下一个receiver接收器。
receiver接收器也可从丢掉广播,使该广播不在传送给别的接收器。
可以通过在intent-filter中设置 android:priority 属性来设置receiver的优先级。
优先级相同的receiver其执行顺序不确定。
注意1:虽然发送广播和Context.startActivity()都使用了Intent。但是他们的机制是完全不一样的。
注意2:只有Orderedbroadcasts(有序广播)才能把结果传给下一个receiver接收器。
才能使用abortBroadcast()丢掉广播
BroadcastReceiver的生命周期:
BroadcastReceiver的生命周期非常短暂的,而且是消息一到达则创建执行完毕就立刻销毁的.
当函数onReceive(Context,Intent)返回后系统就认为该对象已经被完成,不会在使用它了,系统立刻销毁该对象。
因此它的生命周期很短,不易做太多的事情,也不要做任何异步操作。
<wbr style="line-height:25px"><span style="color:#0000ff; line-height:25px">对于耗时的事情通常放在</span><span style="color:#993300; line-height:25px">Service</span><span style="color:#0000ff; line-height:25px">来处理,在BroadcastReceiver调用</span><span style="color:#993300; line-height:25px">Context.startService()</span><span style="color:#0000ff; line-height:25px">启动一个服务来处理耗时的工作。</span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px">注意1</span>:BroadcastReceiver是在intent匹配后再实例化,而且每次都是重新实例化的。<br style="line-height:25px"><span style="line-height:25px">注意2</span>:因为onReceive是运行在themainthreadofitsprocess,所有如果该函数的运行时间超过了10秒,<br style="line-height:25px"> 系统就会认为该程序没响应,会弹出杀死该程序的选择框。<br style="line-height:25px"><span style="line-height:25px">注意3</span>:程序中手动注册的BroadcastReceiver,并不会重新实例化的。具体见实例1<br style="line-height:25px"> Permissions<br style="line-height:25px"><span style="color:#003366; line-height:25px">如果在发送广播的时候权限项非null,那么接收接收广播的应用程序必须要有相对应的权限。<br style="line-height:25px"> 可以自定义Permissions,但是Permissions首先要在AndroidManifest.xml中进行申明</span><br style="line-height:25px"><span style="line-height:25px">示例1:</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Intentintent=newIntent("com.teleca.action.HELLO");<br style="line-height:25px"> sendBroadcast(intent);</span><br style="line-height:25px"> AndroidManifest.xml文件<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><receiverandroid:name="MyReceiver"><br style="line-height:25px"> <intent-filter><br style="line-height:25px"></span><span style="color:#0000ff; line-height:25px"><actionandroid:name="com.teleca.action.HELLO"/></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></intent-filter></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></receiver></span><br style="line-height:25px"> BroadcastReceiver代码<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#ff6600; line-height:25px">MyReceiver</span>extendsBroadcastReceiver{<br style="line-height:25px"> finalstaticStringACTION_HELLO="com.teleca.action.HELLO";<br style="line-height:25px"> finalstaticStringtag="hubin";<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span>onReceive(Contextcontext,Intentintent)<br style="line-height:25px"> {<br style="line-height:25px"><span style="color:#993300; line-height:25px">if</span>(<span style="color:#0000ff; line-height:25px">intent.getAction().equals(ACTION_HELLO)</span>)<br style="line-height:25px"> {<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"hello,robin");</span><br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"><span style="line-height:25px">示例2:</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Stringaction="com.teleca.action.HELLO";<br style="line-height:25px"> Stringpermission="com.teleca.permission.ACCESS";<br style="line-height:25px"> Intentintent=newIntent(action);<br style="line-height:25px"> sendBroadcast(intent,permission);</span><br style="line-height:25px"> BroadcastReceiver代码和例1相同.<br style="line-height:25px"> AndroidManifest.xml文件<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><?xmlversion="1.0"encoding="utf-8"?></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><manifestxmlns:android="http://schemas.android.com/apk/res/android"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">package="com.teleca"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">android:versionCode="1"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">android:versionName="1.0"></span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px"><permissionandroid:name="com.teleca.permission.ACCESS"<br style="line-height:25px"> android:label="@string/MyPermission"<br style="line-height:25px"> android:description="@string/MyPermission"<br style="line-height:25px"> android:protectionLevel="dangerous"/></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px"><!--这里申明权限--></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><applicationandroid:icon="@drawable/icon"android:label="@string/app_name"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><activityandroid:name=".Hello"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">android:label="@string/app_name"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><intent-filter></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><actionandroid:name="android.intent.action.MAIN"/></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><categoryandroid:name="android.intent.category.LAUNCHER"/></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></intent-filter></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></activity></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><activityandroid:name=".Hello2"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">android:label="@string/app_name"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><intent-filter></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><actionandroid:name="com.teleca.action.MAIN"/></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><categoryandroid:name="com.teleca.category.DEFAULT"/></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><categoryandroid:name="android.intent.category.DEFAULT"/></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><dataandroid:mimeType="vnd.android.cursor.item/vnd.google.note"android:scheme="weather"android:host="com.msi.manning"/></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></intent-filter></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></activity></span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px"><receiverandroid:name="MyReceiver"><br style="line-height:25px"> <intent-filter><br style="line-height:25px"> <actionandroid:name="com.teleca.action.HELLO"/><br style="line-height:25px"> </intent-filter></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px"></receiver></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></application></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><uses-sdkandroid:minSdkVersion="7"/></span><br style="line-height:25px"><br style="line-height:25px"><span style="color:#0000ff; line-height:25px"><uses-permissionandroid:name="com.teleca.permission.ACCESS"></uses-permission></span><br style="line-height:25px"><span style="color:#808080; line-height:25px"><!--这里使用申明的权限--></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></manifest></span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">实例1</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">packagecom.teleca;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.app.Activity;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.content.BroadcastReceiver;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.content.Context;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.content.Intent;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.content.IntentFilter;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.os.Bundle;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.util.Log;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.view.View;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.view.View.OnClickListener;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.widget.Button;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.widget.LinearLayout;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#ff6600; line-height:25px">Hello</span><span style="color:#993300; line-height:25px">extends</span><span style="color:#3366ff; line-height:25px">Activity{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Stringtag="hubin";</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**Calledwhentheactivityisfirstcreated.*/<br style="line-height:25px"> @Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onCreate</span><span style="color:#3366ff; line-height:25px">(BundlesavedInstanceState){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onCreate(savedInstanceState);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">setContentView(R.layout.main);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Buttonbutton=(Button)findViewById(R.id.Button01);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener=newOnClickListener(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidonClick(Viewv){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Intentintent=newIntent(Hello.this,Hello2.class);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">startActivity(intent);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px"><wbr style="line-height:25px">finish();</wbr></span><span style="color:#808080; line-height:25px">//<wbr style="line-height:25px">@2</wbr></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">IntentFilterfilter=newIntentFilter();<br style="line-height:25px"> filter.addAction("Hello");<br style="line-height:25px"> this.registerReceiver(receiver,filter);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"createone1");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">protectedvoidonDestroy()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onDestroy();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"oneDestroy");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#0000ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#0000ff; line-height:25px">BroadcastReceiver</span><span style="color:#993300; line-height:25px">receiver</span><span style="color:#0000ff; line-height:25px">=newBroadcastReceiver(){<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#0000ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onReceive</span><span style="color:#0000ff; line-height:25px">(Contextcontext,Intentintent){<br style="line-height:25px"> Log.i(tag,"receiver:"+this);//@1<br style="line-height:25px"> }<wbr style="line-height:25px"><br style="line-height:25px"> };</wbr></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> 文件2:Hello2.java<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.view.View;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.view.View.OnClickListener;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.widget.Button;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#ff6600; line-height:25px">Hello2</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">extends</span><span style="color:#3366ff; line-height:25px">Activity{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Stringtag="hubin";</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**Calledwhentheactivityisfirstcreated.*/<br style="line-height:25px"> @Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onCreate</span><span style="color:#3366ff; line-height:25px">(BundlesavedInstanceState){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onCreate(savedInstanceState);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">setContentView(R.layout.main2);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Buttonbutton=(Button)findViewById(R.id.Button01);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener=newOnClickListener(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidonClick(Viewv){</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Intentintent=newIntent("Hello");<br style="line-height:25px"> sendBroadcast(intent);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">button.setOnClickListener(listener);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1:</wbr></span>程序中手动注册的BroadcastReceiver,并不会重新实例化的。这点和AndroidManifest.xml不一样<br style="line-height:25px"><span style="line-height:25px">注意2:</span>它的存活时间是要一直到Activity死亡。如果调用了finish(),该Activity就死亡,receiver也同时死亡掉。</wbr></wbr></wbr></wbr>
java.lang.Object
android.content.BroadcastReceiver
BaseclassforcodethatwillreceiveintentssentbysendBroadcast().
YoucaneitherdynamicallyregisteraninstanceofthisclasswithContext.registerReceiver()
orstaticallypublishanimplementationthroughthe<receiver>taginyourAndroidManifest.xml.
Note:IfregisteringareceiverinyourActivity.onResume()implementation,youshouldunregisteritinActivity.onPause().
(Youwon'treceiveintentswhenpaused,andthiswillcutdownonunnecessarysystemoverhead).
DonotunregisterinActivity.onSaveInstanceState(),becausethiswon'tbecallediftheusermovesbackinthehistorystack.
该类主要用来接收sendBroadcast()发出的intent。
可以通过在代码中用Context.registerReceiver()的形式动态注册BroadcastReceiver。
也可以在AndroidManifest.xml中通过<receiver>来注册BroadcastReceiver。
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
<nobr style="line-height:21px">final void</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#abortBroadcast()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">abortBroadcast</a></span>()</nobr>
Sets the flag indicating that this receiver should abort the current broadcast; only works with broadcasts sent through
Context.sendOrderedBroadcast .
| ||||||||||
<nobr style="line-height:21px">final void</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#clearAbortBroadcast()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">clearAbortBroadcast</a></span>()</nobr>
Clears the flag indicating that this receiver should abort the current broadcast.
| ||||||||||
<nobr style="line-height:21px">final boolean</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#getAbortBroadcast()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getAbortBroadcast</a></span>()</nobr>
Returns the flag indicating whether or not this receiver should abort the current broadcast.
| ||||||||||
<nobr style="line-height:21px">final boolean</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#getDebugUnregister()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getDebugUnregister</a></span>()</nobr>
Return the last value given to
setDebugUnregister(boolean) .
| ||||||||||
<nobr style="line-height:21px">final int</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#getResultCode()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getResultCode</a></span>()</nobr>
Retrieve the current result code, as set by the previous receiver.
| ||||||||||
<nobr style="line-height:21px">final<a rel="nofollow" href="http://developer.android.com/reference/java/lang/String.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">String</a></nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#getResultData()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getResultData</a></span>()</nobr>
Retrieve the current result data, as set by the previous receiver.
| ||||||||||
<nobr style="line-height:21px">final<a rel="nofollow" href="http://developer.android.com/reference/android/os/Bundle.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Bundle</a></nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#getResultExtras(boolean)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">getResultExtras</a></span>(boolean makeMap)</nobr>
Retrieve the current result extra data, as set by the previous receiver.
| ||||||||||
<nobr style="line-height:21px">final<a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.PendingResult.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">BroadcastReceiver.PendingResult</a></nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#goAsync()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">goAsync</a></span>()</nobr>
This can be called by an application in
onReceive(Context, Intent) to allow it to keep the broadcast active after returning from that function.
| ||||||||||
<nobr style="line-height:21px">final boolean</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#isInitialStickyBroadcast()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">isInitialStickyBroadcast</a></span>()</nobr>
Returns true if the receiver is currently processing the initial value of a sticky broadcast -- that is, the value that was last broadcast and is currently held in the sticky cache, so this is not directly the result of a broadcast right now.
| ||||||||||
<nobr style="line-height:21px">final boolean</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#isOrderedBroadcast()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">isOrderedBroadcast</a></span>()</nobr>
Returns true if the receiver is currently processing an ordered broadcast.
| ||||||||||
<nobr style="line-height:21px">abstract void</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive(android.content.Context,%20android.content.Intent)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">onReceive</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/content/Context.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Context</a>context,<a rel="nofollow" href="http://developer.android.com/reference/android/content/Intent.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Intent</a>intent)</nobr>
This method is called when the BroadcastReceiver is receiving an Intent broadcast.
| ||||||||||
<nobr style="line-height:21px"><a rel="nofollow" href="http://developer.android.com/reference/android/os/IBinder.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">IBinder</a></nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#peekService(android.content.Context,%20android.content.Intent)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">peekService</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/content/Context.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Context</a>myContext,<a rel="nofollow" href="http://developer.android.com/reference/android/content/Intent.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Intent</a>service)</nobr>
Provide a binder to an already-running service.
| ||||||||||
<nobr style="line-height:21px">final void</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#setDebugUnregister(boolean)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setDebugUnregister</a></span>(boolean debug)</nobr>
Control inclusion of debugging help for mismatched calls to {@ Context#registerReceiver(BroadcastReceiver, IntentFilter) Context.registerReceiver()}.
| ||||||||||
<nobr style="line-height:21px">final void</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#setOrderedHint(boolean)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setOrderedHint</a></span>(boolean isOrdered)</nobr>
For internal use, sets the hint about whether this BroadcastReceiver is running in ordered mode.
| ||||||||||
<nobr style="line-height:21px">final void</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#setResult(int,%20java.lang.String,%20android.os.Bundle)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setResult</a></span>(int code,<a rel="nofollow" href="http://developer.android.com/reference/java/lang/String.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">String</a>data,<a rel="nofollow" href="http://developer.android.com/reference/android/os/Bundle.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Bundle</a>extras)</nobr>
Change all of the result data returned from this broadcasts; only works with broadcasts sent through
Context.sendOrderedBroadcast .
| ||||||||||
<nobr style="line-height:21px">final void</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#setResultCode(int)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setResultCode</a></span>(int code)</nobr>
Change the current result code of this broadcast; only works with broadcasts sent through
Context.sendOrderedBroadcast .
| ||||||||||
<nobr style="line-height:21px">final void</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#setResultData(java.lang.String)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setResultData</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/lang/String.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">String</a>data)</nobr>
Change the current result data of this broadcast; only works with broadcasts sent through
Context.sendOrderedBroadcast .
| ||||||||||
<nobr style="line-height:21px">final void</nobr> | <nobr style="line-height:21px"><span style="line-height:21px; margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#setResultExtras(android.os.Bundle)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">setResultExtras</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/android/os/Bundle.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Bundle</a>extras)</nobr>
Change the current result extras of this broadcast; only works with broadcasts sent through
Context.sendOrderedBroadcast .
|
Normalbroadcasts( 普通广播 ):It(sentwithContext.sendBroadcast)arecompletelyasynchronous.
Allreceiversofthebroadcastareruninanundefinedorder,oftenatthesametime.
Thisismoreefficient,butmeansthatreceiverscannotusetheresultorabortAPIsincludedhere.
该广播通过 Context.sendBroadcast 这种发送发送。它是完全异步的。
所有的receivers接收器的执行顺序不确定。因此,所有的receivers接收器接收broadcast的顺序不确定。
这种方式效率更高。但是receivers接收器无法使用theresultorabortAPIs
Orderedbroadcasts( 有序广播):It(sentwithContext.sendOrderedBroadcast)aredeliveredtoonereceiveratatime.
Aseachreceiverexecutesinturn,itcanpropagatearesulttothenextreceiver,
oritcancompletelyabortthebroadcastsothatitwon'tbepassedtootherreceivers.
Theorderreceiversrunincanbecontrolledwiththe android:priorityattribute
ofthematchingintent-filter;
receiverswiththesameprioritywillberuninanarbitraryorder.
该广播是通过 Context.sendOrderedBroadcast 来发送。所有的receiver依次执行。
receiver接收器可以把结果传给下一个receiver接收器。
receiver接收器也可从丢掉广播,使该广播不在传送给别的接收器。
可以通过在intent-filter中设置 android:priority 属性来设置receiver的优先级。
优先级相同的receiver其执行顺序不确定。
注意1:虽然发送广播和Context.startActivity()都使用了Intent。但是他们的机制是完全不一样的。
注意2:只有Orderedbroadcasts(有序广播)才能把结果传给下一个receiver接收器。
才能使用abortBroadcast()丢掉广播
BroadcastReceiver的生命周期:
BroadcastReceiver的生命周期非常短暂的,而且是消息一到达则创建执行完毕就立刻销毁的.
当函数onReceive(Context,Intent)返回后系统就认为该对象已经被完成,不会在使用它了,系统立刻销毁该对象。
因此它的生命周期很短,不易做太多的事情,也不要做任何异步操作。
<wbr style="line-height:25px"><span style="color:#0000ff; line-height:25px">对于耗时的事情通常放在</span><span style="color:#993300; line-height:25px">Service</span><span style="color:#0000ff; line-height:25px">来处理,在BroadcastReceiver调用</span><span style="color:#993300; line-height:25px">Context.startService()</span><span style="color:#0000ff; line-height:25px">启动一个服务来处理耗时的工作。</span><wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px">注意1</span>:BroadcastReceiver是在intent匹配后再实例化,而且每次都是重新实例化的。<br style="line-height:25px"><span style="line-height:25px">注意2</span>:因为onReceive是运行在themainthreadofitsprocess,所有如果该函数的运行时间超过了10秒,<br style="line-height:25px"> 系统就会认为该程序没响应,会弹出杀死该程序的选择框。<br style="line-height:25px"><span style="line-height:25px">注意3</span>:程序中手动注册的BroadcastReceiver,并不会重新实例化的。具体见实例1<br style="line-height:25px"> Permissions<br style="line-height:25px"><span style="color:#003366; line-height:25px">如果在发送广播的时候权限项非null,那么接收接收广播的应用程序必须要有相对应的权限。<br style="line-height:25px"> 可以自定义Permissions,但是Permissions首先要在AndroidManifest.xml中进行申明</span><br style="line-height:25px"><span style="line-height:25px">示例1:</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Intentintent=newIntent("com.teleca.action.HELLO");<br style="line-height:25px"> sendBroadcast(intent);</span><br style="line-height:25px"> AndroidManifest.xml文件<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><receiverandroid:name="MyReceiver"><br style="line-height:25px"> <intent-filter><br style="line-height:25px"></span><span style="color:#0000ff; line-height:25px"><actionandroid:name="com.teleca.action.HELLO"/></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></intent-filter></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></receiver></span><br style="line-height:25px"> BroadcastReceiver代码<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#ff6600; line-height:25px">MyReceiver</span>extendsBroadcastReceiver{<br style="line-height:25px"> finalstaticStringACTION_HELLO="com.teleca.action.HELLO";<br style="line-height:25px"> finalstaticStringtag="hubin";<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span>onReceive(Contextcontext,Intentintent)<br style="line-height:25px"> {<br style="line-height:25px"><span style="color:#993300; line-height:25px">if</span>(<span style="color:#0000ff; line-height:25px">intent.getAction().equals(ACTION_HELLO)</span>)<br style="line-height:25px"> {<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"hello,robin");</span><br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"><span style="line-height:25px">示例2:</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Stringaction="com.teleca.action.HELLO";<br style="line-height:25px"> Stringpermission="com.teleca.permission.ACCESS";<br style="line-height:25px"> Intentintent=newIntent(action);<br style="line-height:25px"> sendBroadcast(intent,permission);</span><br style="line-height:25px"> BroadcastReceiver代码和例1相同.<br style="line-height:25px"> AndroidManifest.xml文件<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><?xmlversion="1.0"encoding="utf-8"?></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><manifestxmlns:android="http://schemas.android.com/apk/res/android"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">package="com.teleca"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">android:versionCode="1"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">android:versionName="1.0"></span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px"><permissionandroid:name="com.teleca.permission.ACCESS"<br style="line-height:25px"> android:label="@string/MyPermission"<br style="line-height:25px"> android:description="@string/MyPermission"<br style="line-height:25px"> android:protectionLevel="dangerous"/></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px"><!--这里申明权限--></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><applicationandroid:icon="@drawable/icon"android:label="@string/app_name"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><activityandroid:name=".Hello"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">android:label="@string/app_name"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><intent-filter></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><actionandroid:name="android.intent.action.MAIN"/></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><categoryandroid:name="android.intent.category.LAUNCHER"/></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></intent-filter></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></activity></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><activityandroid:name=".Hello2"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">android:label="@string/app_name"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><intent-filter></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><actionandroid:name="com.teleca.action.MAIN"/></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><categoryandroid:name="com.teleca.category.DEFAULT"/></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><categoryandroid:name="android.intent.category.DEFAULT"/></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><dataandroid:mimeType="vnd.android.cursor.item/vnd.google.note"android:scheme="weather"android:host="com.msi.manning"/></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></intent-filter></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></activity></span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px"><receiverandroid:name="MyReceiver"><br style="line-height:25px"> <intent-filter><br style="line-height:25px"> <actionandroid:name="com.teleca.action.HELLO"/><br style="line-height:25px"> </intent-filter></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px"></receiver></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></application></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"><uses-sdkandroid:minSdkVersion="7"/></span><br style="line-height:25px"><br style="line-height:25px"><span style="color:#0000ff; line-height:25px"><uses-permissionandroid:name="com.teleca.permission.ACCESS"></uses-permission></span><br style="line-height:25px"><span style="color:#808080; line-height:25px"><!--这里使用申明的权限--></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></manifest></span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">实例1</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">packagecom.teleca;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.app.Activity;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.content.BroadcastReceiver;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.content.Context;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.content.Intent;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.content.IntentFilter;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.os.Bundle;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.util.Log;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.view.View;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.view.View.OnClickListener;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.widget.Button;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.widget.LinearLayout;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#ff6600; line-height:25px">Hello</span><span style="color:#993300; line-height:25px">extends</span><span style="color:#3366ff; line-height:25px">Activity{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Stringtag="hubin";</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**Calledwhentheactivityisfirstcreated.*/<br style="line-height:25px"> @Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onCreate</span><span style="color:#3366ff; line-height:25px">(BundlesavedInstanceState){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onCreate(savedInstanceState);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">setContentView(R.layout.main);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Buttonbutton=(Button)findViewById(R.id.Button01);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener=newOnClickListener(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidonClick(Viewv){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Intentintent=newIntent(Hello.this,Hello2.class);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">startActivity(intent);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px"><wbr style="line-height:25px">finish();</wbr></span><span style="color:#808080; line-height:25px">//<wbr style="line-height:25px">@2</wbr></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">IntentFilterfilter=newIntentFilter();<br style="line-height:25px"> filter.addAction("Hello");<br style="line-height:25px"> this.registerReceiver(receiver,filter);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"createone1");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">protectedvoidonDestroy()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onDestroy();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"oneDestroy");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#0000ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#0000ff; line-height:25px">BroadcastReceiver</span><span style="color:#993300; line-height:25px">receiver</span><span style="color:#0000ff; line-height:25px">=newBroadcastReceiver(){<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#0000ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onReceive</span><span style="color:#0000ff; line-height:25px">(Contextcontext,Intentintent){<br style="line-height:25px"> Log.i(tag,"receiver:"+this);//@1<br style="line-height:25px"> }<wbr style="line-height:25px"><br style="line-height:25px"> };</wbr></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> 文件2:Hello2.java<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.view.View;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.view.View.OnClickListener;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.widget.Button;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#ff6600; line-height:25px">Hello2</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">extends</span><span style="color:#3366ff; line-height:25px">Activity{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Stringtag="hubin";</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**Calledwhentheactivityisfirstcreated.*/<br style="line-height:25px"> @Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">onCreate</span><span style="color:#3366ff; line-height:25px">(BundlesavedInstanceState){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onCreate(savedInstanceState);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">setContentView(R.layout.main2);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Buttonbutton=(Button)findViewById(R.id.Button01);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener=newOnClickListener(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidonClick(Viewv){</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Intentintent=newIntent("Hello");<br style="line-height:25px"> sendBroadcast(intent);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">button.setOnClickListener(listener);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意1:</wbr></span>程序中手动注册的BroadcastReceiver,并不会重新实例化的。这点和AndroidManifest.xml不一样<br style="line-height:25px"><span style="line-height:25px">注意2:</span>它的存活时间是要一直到Activity死亡。如果调用了finish(),该Activity就死亡,receiver也同时死亡掉。</wbr></wbr></wbr></wbr>