Osheep

时光不回头,当下最重要。

[原创]Android的BroadcastReceiver简介

1、概念:BroadcastReceiver也是Android的四大组件之一,它本质上是一个全局的监听器,用于监听系统全局的广播消息,实现系统中不同组件之间的通信。只要存在与之匹配的Intent被广播出来,BroadcastReceiver总会被激发。指定BroadcastReceiver的Intent既可以在代码中指定,也可以在AndroidManifest中指定。

《[原创]Android的BroadcastReceiver简介》

每次Broadcast事件发生后,系统就会创建对应的BroadcastReceiver实例,并自动触发它的OnReceive()方法,此方法执行完后,BroadcastReceiver实例就会被销毁。另外,如果OnReceive方法不能在10秒内执行完成,Android会认为该程序无响应,所以不要在此方法里执行一些耗时的操作。如果确实根据Broadcast来完成一些比较耗时的操作,可以考虑通过Intent启动一个Service来完成。

2、分类:Broadcast分为普通广播和有序广播。

普通广播:完全异步的,可以在同一时刻(逻辑上)被所有接收者接收到,消息传递效率高。缺点是接收者不能将处理结果传递给下一个接收者,而且无法终止广播。

有序广播:接收者是按预先声明的优先级依次接受广播,接收者可以传递数据给下一个接收者,也可以终止广播继续传播。

下面通过一个示例来演示它们的使用:

本示例在配置优先级时,让FristReceiver的优先级低于SecondReceiver,所以SecondReceiver会先接收到有序广播。

Activity:

[java]
package com.home.activity; 
 
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; 
 
import com.home.broadcastreceivertest.R; 
 
public class SendBroadActivity extends Activity { 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        Button sendCommonBroadBtn = (Button) findViewById(R.id.main_btn_send_commonbroad); 
        Button sendSortBroadBtn = (Button) findViewById(R.id.main_btn_send_sortbroad); 
        sendCommonBroadBtn.setOnClickListener(new OnClickListener() { 
 
            @Override 
            public void onClick(View v) { 
                // 发送普通广播  
                Intent intent = new Intent(); 
                intent.setAction(“com.home.Common_BroadCast”); 
                intent.putExtra(“msg”, “发普通广播啦!!!”); 
                sendBroadcast(intent); 
            } 
        }); 
        sendSortBroadBtn.setOnClickListener(new OnClickListener() { 
 
            @Override 
            public void onClick(View v) { 
                // 发送有序广播  
                Intent intent = new Intent(); 
                intent.setAction(“com.home.Sort_BroadCast”); 
                intent.putExtra(“msg”, “发有序广播啦!!!”); 
                sendOrderedBroadcast(intent, null); 
            } 
        }); 
    } 

package com.home.activity;

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;

import com.home.broadcastreceivertest.R;

public class SendBroadActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Button sendCommonBroadBtn = (Button) findViewById(R.id.main_btn_send_commonbroad);
  Button sendSortBroadBtn = (Button) findViewById(R.id.main_btn_send_sortbroad);
  sendCommonBroadBtn.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // 发送普通广播
    Intent intent = new Intent();
    intent.setAction(“com.home.Common_BroadCast”);
    intent.putExtra(“msg”, “发普通广播啦!!!”);
    sendBroadcast(intent);
   }
  });
  sendSortBroadBtn.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // 发送有序广播
    Intent intent = new Intent();
    intent.setAction(“com.home.Sort_BroadCast”);
    intent.putExtra(“msg”, “发有序广播啦!!!”);
    sendOrderedBroadcast(intent, null);
   }
  });
 }
}

FristReceiver:

[java]
package com.home.receiver; 
 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.Toast; 
 
public class FristReceiver extends BroadcastReceiver { 
 
    @Override 
    public void onReceive(Context context, Intent intent) { 
        // 如果收到的是普通广播  
        if (“com.home.Common_BroadCast”.equals(intent.getAction())) { 
            String msg = intent.getStringExtra(“msg”); 
            Toast.makeText(context, “FristReceiver收到的普通广播为:” + msg, 
                    Toast.LENGTH_SHORT).show(); 
        } 
        // 如果收到的是有序广播  
        if (“com.home.Sort_BroadCast”.equals(intent.getAction())) { 
            String msg = intent.getStringExtra(“msg”); 
            // 取出上一个广播设置的数据  
            Bundle bundle = getResultExtras(true); 
            String PassMsg = “”; 
            if (bundle != null) { 
                PassMsg = bundle.getString(“message”); 
            } 
            String resultMsg = msg + “n” + PassMsg; 
            Toast.makeText(context, “FristReceiver收到的有序广播为:” + resultMsg, 
                    Toast.LENGTH_SHORT).show(); 
        } 
 
    } 
 

package com.home.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class FristReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
  // 如果收到的是普通广播
  if (“com.home.Common_BroadCast”.equals(intent.getAction())) {
   String msg = intent.getStringExtra(“msg”);
   Toast.makeText(context, “FristReceiver收到的普通广播为:” + msg,
     Toast.LENGTH_SHORT).show();
  }
  // 如果收到的是有序广播
  if (“com.home.Sort_BroadCast”.equals(intent.getAction())) {
   String msg = intent.getStringExtra(“msg”);
   // 取出上一个广播设置的数据
   Bundle bundle = getResultExtras(true);
   String PassMsg = “”;
   if (bundle != null) {
    PassMsg = bundle.getString(“message”);
   }
   String resultMsg = msg + “n” + PassMsg;
   Toast.makeText(context, “FristReceiver收到的有序广播为:” + resultMsg,
     Toast.LENGTH_SHORT).show();
  }

 }

}
SecondReceiver:

[java]
package com.home.receiver; 
 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.Toast; 
 
public class SecondReceiver extends BroadcastReceiver { 
 
    @Override 
    public void onReceive(Context context, Intent intent) { 
        // 如果收到的是普通广播  
        if (“com.home.Common_BroadCast”.equals(intent.getAction())) { 
            String msg = intent.getStringExtra(“msg”); 
            Toast.makeText(context, “SecondReceiver收到的普通广播为:” + msg, 
                    Toast.LENGTH_SHORT).show(); 
        } 
        // 如果收到的是有序广播  
        if (“com.home.Sort_BroadCast”.equals(intent.getAction())) { 
            String msg = intent.getStringExtra(“msg”); 
            Toast.makeText(context, “SecondReceiver收到的有序广播为:” + msg, 
                    Toast.LENGTH_SHORT).show(); 
            Bundle bundle = new Bundle(); 
            bundle.putString(“message”, “传递过程中附加了信息”); 
            // 设置数据到下一个接受者  
            setResultExtras(bundle); 
            // 终止广播传递给下一个接受者  
            // abortBroadcast();  
        } 
    } 
 

package com.home.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class SecondReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
  // 如果收到的是普通广播
  if (“com.home.Common_BroadCast”.equals(intent.getAction())) {
   String msg = intent.getStringExtra(“msg”);
   Toast.makeText(context, “SecondReceiver收到的普通广播为:” + msg,
     Toast.LENGTH_SHORT).show();
  }
  // 如果收到的是有序广播
  if (“com.home.Sort_BroadCast”.equals(intent.getAction())) {
   String msg = intent.getStringExtra(“msg”);
   Toast.makeText(context, “SecondReceiver收到的有序广播为:” + msg,
     Toast.LENGTH_SHORT).show();
   Bundle bundle = new Bundle();
   bundle.putString(“message”, “传递过程中附加了信息”);
   // 设置数据到下一个接受者
   setResultExtras(bundle);
   // 终止广播传递给下一个接受者
   // abortBroadcast();
  }
 }

}
配置receiver和优先级:

[html]
<receiver android:name=”com.home.receiver.FristReceiver”> 
           <intent-filter  android:priority=”50″> 
               <action android:name=”com.home.Sort_BroadCast”/> 
                 <action android:name=”com.home.Common_BroadCast”/> 
           </intent-filter> 
       </receiver> 
         <receiver android:name=”com.home.receiver.SecondReceiver”> 
           <intent-filter android:priority=”100″> 
               <action android:name=”com.home.Sort_BroadCast”/> 
                <action android:name=”com.home.Common_BroadCast”/> 
           </intent-filter> 

 <receiver android:name=”com.home.receiver.FristReceiver”>
            <intent-filter  android:priority=”50″>
                <action android:name=”com.home.Sort_BroadCast”/>
                  <action android:name=”com.home.Common_BroadCast”/>
            </intent-filter>
        </receiver>
          <receiver android:name=”com.home.receiver.SecondReceiver”>
            <intent-filter android:priority=”100″>
                <action android:name=”com.home.Sort_BroadCast”/>
                 <action android:name=”com.home.Common_BroadCast”/>
            </intent-filter>
 

 


 

点赞