Android-发送有序广播

1.功能描述:实现拦截一条有序广播。

2.技术要点:通过sendOrderedBroadcast()方法发送一条有序广播。

(1)用户交互界面的设计与实现

(2)界面交互代码的设计与实现

(3)创建3个广播接受者:

MyBroadcastReceiverOne.java

MyBroadcastReceiverTwo.java

MyBroadcastReceiverThree.java

3.实现步骤:(4)设置优先级广播接受者的优先级

4.有序广播的工作流程:

发送一条广播——>广播接受器1—|—>广播接收器2—|—>广播接收器3

实例:

1、编写用户交互界面

activity_main.xml



    

2、点击发送有序广播按钮时事件处理

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void send(View view){
        Intent intent=new Intent();
        //定义广播的事件类型
public class MyBroadcastReceiverThree extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyBroadcastReceiverThree"," Three接收到广播");
    }
}

intent.setAction("Intercept_Stitch"); //发送广播 sendOrderedBroadcast(intent,null); }}
 
  


 3、创建广播接收者MyBroadcastReceiverOne 
  

public class MyBroadcastReceiverOne extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyBroadcastReceiverOne"," One接收到广播");
    }
}

创建广播接收者MyBroadcastReceiverTwo

public class MyBroadcastReceiverTwo extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyBroadcastReceiverTwo"," Two接收到广播");
    }
}
创建广播接收者MyBroadcastReceiverThree

public class MyBroadcastReceiverThree extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyBroadcastReceiverThree"," Three接收到广播");
    }
}



你可能感兴趣的:(Android-发送有序广播)