用安卓发送有序广播

实用安卓编写程序发送有序广播。

主布局activity_main.xml:




    
        
            
                

                
            
        

        
            
                
            
        

        
            
                
            

        
        
            
                
            

        

    

MainActivity.java:
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();
        //定义广播事件类型
        intent.setAction("Intercept_Stitch");
        // 发送广播
        sendOrderedBroadcast(intent,null);
    }
}
MyReceiveOne.java:
public class MyReceiverOne extends BroadcastReceiver {
    public MyReceiverOne() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyBroadcaseReceiverOne","自定义广播One");
    }
}
MyReceiveTwo.java:
public class MyReceiverTwo extends BroadcastReceiver {
    public MyReceiverTwo() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyBroadcaseReceiverTwo","自定义广播Two");
        abortBroadcast();//拦截有序广播
        Log.i("MyBroadcaseReceiverTwo","有序广播Two,广播被我冻结");
    }
}
MyReceiveThree.java:
public class MyReceiverThree extends BroadcastReceiver {
    public MyReceiverThree() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyBroadcaseReceiverThree","自定义广播Three");
    }
}
效果图如下: 用安卓发送有序广播_第1张图片

结果图:

可知广播在优先级最低的MyReceiveTwo中被终结

你可能感兴趣的