标签:broadcastreceiver setresultextras android四大组件 有序广播 无序广播
业精于勤,荒于嬉;行成于思,毁于随。---韩愈
在Android开发中BroadcastReceiver的使用是非常广泛的它也是Android的四大组件之一,翻译成汉语就是:"广播接收者",那么什么是广播呢?举个日常生活的例子,在农村大队里村干部这里会有一个大喇叭,用来发送广播通知各种事情,假如由于小花超生通知小花和小明来开会村干部就会通过大喇叭说:”喂,咱庄上的人注意了,小花和小明请以最快的速度来开会"。这个广播发出之后,首先这个庄上的所有人都听到了广播,村民们根据广播的内容选择去还是不去,小花和小明听到广播后开始分析:"小花和小明去开会,我是小花,我应该去开会"小明也是如此,村民们虽然听到了广播,但是分析后发现跟我没关系啊,于是继续干自己的事情。这是我们生活中的广播的形式,那么在android中广播是怎么一回事呢?一起来揭开它的面纱吧。
<receiver
android:name=".broadcastreceiver.FirstBroadcastReceiver"> <intent-filter android:priority="Integer" > <action android:name="com.example.BroadcastReceiver" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>
<receiver
android:name = ".broadcastreceiver.FirstBroadcastReceiver"> <intent-filter> < action android:name ="com.example.BroadcastReceiver" /> < category android:name ="android.intent.category.DEFAULT" /> </intent-filter> </receiver>配置了以上信息之后,只要是com.example.BroadcastReceiver这个地址的广播,广播接收者都能够接收的到。注意,这种方式的注册是常驻型的,也就是说当应用关闭后,如果有广播信息传来,广播接受者也会被系统调用而自动运行。
FirstBroadcastReceiver firstBroadcastReceiver= new FirstBroadcastReceiver(); IntentFilter intentFilter= new IntentFilter(); intentFilter.addAction( "com.example.BroadcastReceiver" ); registerReceiver(firstBroadcastReceiver,intentFilter);可能有的朋友会问,为什么registerReceiver和SendBroadcastReceiver在Activity和Service中可以直接用,我们看看它们的继承关系图:
@Override protected void onDestroy() { super.onDestroy(); unregisterReceiver( firstBroadcastReceiver ); }
package com.example.broadcastreceiverpractice.broadcastreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class FirstBroadcastReceiver extends BroadcastReceiver { private static final String TAG = "FirstBroadcastReceiver" ; @Override public void onReceive(Context context, Intent intent) { String getFromBroadcast = intent.getStringExtra("key" ); Log. i( TAG,getFromBroadcast); } }在onReceive方法中我们通过获得广播传过来的数据来进行测试是否收到了我们所需要的广播,进行到这儿还有更重要的一步,那就是注册广播假如我们不注册广播的话,我们想向这个广播接收者发消息的话就无法发送,因为找不到和这个广播对应的东西,注册广播可以分为两种上面已经提到了我们再来通过实例说一遍
<receiver
android:name =".broadcastreceiver.FirstBroadcastReceiver" > <intent-filter> <action android:name ="com.example.BroadcastReceiver" /> <category android:name ="android.intent.category.DEFAULT" /> </intent-filter> </receiver>
package com.example.broadcastreceiverpractice; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button btn_send; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout. activity_main); btn_send=(Button) findViewById(R.id. btn_send); btn_send.setOnClickListener( new OnClickListener() { public void onClick(View v) { //发送一条广播com.example.BroadcastReceiver要和广播接收者在清单文件中配置的相同 Intent intent = new Intent("com.example.BroadcastReceiver" ); intent.putExtra( "key", "MainActivity中发了一条广播" ); sendBroadcast(intent); } }); } }我们点击几次发送按钮在广播接收者中打印内容如下
package com.example.broadcastreceiverpractice; import com.example.broadcastreceiverpractice.broadcastreceiver.FirstBroadcastReceiver; import android.app.Activity; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button btn_send; private FirstBroadcastReceiver firstBroadcastReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout. activity_main); firstBroadcastReceiver = new FirstBroadcastReceiver(); //声明过滤器 IntentFilter intentFilter= new IntentFilter(); intentFilter.addAction( "com.example.BroadcastReceiver" ); //将添加的动作和声明的广播接收者绑定到一起,和我们在清单文件中配置的作用是一样的 registerReceiver( firstBroadcastReceiver,intentFilter); btn_send=(Button) findViewById(R.id. btn_send); btn_send.setOnClickListener( new OnClickListener() { public void onClick(View v) { //发送一条广播com.example.BroadcastReceiver要和广播接收者在清单文件中配置的相同 Intent intent = new Intent("com.example.BroadcastReceiver" ); intent.putExtra( "key", "MainActivity中发了一条广播" ); sendBroadcast(intent); } }); } @Override protected void onDestroy() { super.onDestroy(); //解除注册 unregisterReceiver( firstBroadcastReceiver); } }我们点击几次按钮发送广播打印内容如下
package com.example.broadcastreceiverpractice.broadcastreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class FirstBroadcastReceiver extends BroadcastReceiver { private static final String TAG = "FirstBroadcastReceiver" ; @Override public void onReceive(Context context, Intent intent) { String getFromBroadcast = intent.getStringExtra("key" ); Log. i( TAG,getFromBroadcast); } }
package com.example.broadcastreceiverpractice.broadcastreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class SecondBroadcastReceiver extends BroadcastReceiver { private static final String TAG = "SecondBroadcastReceiver" ; @Override public void onReceive(Context context, Intent intent) { String getFromBroadcast = intent.getStringExtra("key" ); Log. i( TAG,getFromBroadcast); } }
package com.example.broadcastreceiverpractice.broadcastreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class ThirdBroadcastReceiver extends BroadcastReceiver { private static final String TAG = "ThirdBroadcastReceiver" ; @Override public void onReceive(Context context, Intent intent) { String getFromBroadcast = intent.getStringExtra("key" ); Log. i( TAG,getFromBroadcast); } }以上三个广播接收者都采用的是静态注册在清单文件中进行注册,然后在MainActivity中点击发送广播按钮打印日志如下
abortBroadcast();
<receiver android:name =".broadcastreceiver.FirstBroadcastReceiver" > <intent-filter android:priority ="10" > <action android:name ="com.example.BroadcastReceiver" /> <category android:name ="android.intent.category.DEFAULT" /> </intent-filter> </receiver> <receiver android:name =".broadcastreceiver.SecondBroadcastReceiver" > <intent-filter android:priority ="9" > <action android:name ="com.example.BroadcastReceiver" /> <category android:name ="android.intent.category.DEFAULT" /> </intent-filter> </receiver> <receiver android:name =".broadcastreceiver.ThirdBroadcastReceiver" > <intent-filter android:priority ="8" > <action android:name ="com.example.BroadcastReceiver" /> <category android:name ="android.intent.category.DEFAULT" /> </intent-filter> </receiver>
//发送一条广播com.example.BroadcastReceiver要和广播接收者在清单文件中配置的相同 Intent intent =new Intent("com.example.BroadcastReceiver" ); intent.putExtra("key", "MainActivity中发了一条广播" ); sendOrderedBroadcast(intent,null);
所以我们在AndroidMainfest.xml中定义一个权限:
<permission android:protectionLevel="normal" android:name="com.tsingda.broadcast_permission"/>然后添加此权限
<uses-permission android:name="com.tsingda.broadcast_permission" />
abortBroadcast();这个方法然后点击发送按钮打印日志如下
package com.example.broadcastreceiverpractice.broadcastreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; public class FirstBroadcastReceiver extends BroadcastReceiver { private static final String TAG = "FirstBroadcastReceiver" ; @Override public void onReceive(Context context, Intent intent) { String getFromBroadcast = intent.getStringExtra("key" ); Log. i( TAG,getFromBroadcast); Bundle bundle = new Bundle(); bundle.putString( "key", getFromBroadcast+ "@First"); setResultExtras(bundle); } }
package com.example.broadcastreceiverpractice.broadcastreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; public class SecondBroadcastReceiver extends BroadcastReceiver { private static final String TAG = "SecondBroadcastReceiver" ; @Override public void onReceive(Context context, Intent intent) { String getFromBroadcast = getResultExtras(true ).getString("key" ); Log. i( TAG,getFromBroadcast); Bundle bundle = new Bundle(); bundle.putString( "key", getFromBroadcast+ "@Second"); setResultExtras(bundle); } }
package com.example.broadcastreceiverpractice.broadcastreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class ThirdBroadcastReceiver extends BroadcastReceiver { private static final String TAG = "ThirdBroadcastReceiver" ; @Override public void onReceive(Context context, Intent intent) { String getFromBroadcast = getResultExtras(true ).getString("key" ); Log. i( TAG,getFromBroadcast); } }点击发送广播按钮打印日志如下
Android开发之一起来学BroadcastReceiver
标签:broadcastreceiver setresultextras android四大组件 有序广播 无序广播
原文地址:http://blog.csdn.net/dmk877/article/details/45199465