安卓 广播(有序、粘性、EventBus)

有序广播

代码如下:
1.清单文件
安卓 广播(有序、粘性、EventBus)_第1张图片
2.发送广播

Intent intent = new Intent("www.wangpeng");
        intent.putExtra("name","我是有序排序");
        sendOrderedBroadcast(intent,null);

3.接收类

// An highlighted block
 String name = intent.getStringExtra("name");
        Bundle bundle = new Bundle();
        bundle.putString("xiedai","我是携带数据");
        setResultExtras(bundle);
        Log.d("###","我是接受方一:"+name);
// An highlighted block
String name = intent.getStringExtra("name");
        Log.d("###","我是接受方2:"+name);
        Bundle resultExtras = getResultExtras(true);
        String xiedai = resultExtras.getString("xiedai");
        Log.d("###","携带数据2:"+xiedai);
// An highlighted block
String name = intent.getStringExtra("name");
        Bundle resultExtras = getResultExtras(true);
        String xiedai = resultExtras.getString("xiedai");
        Log.d("###","我是接受方3:"+name);
        Log.d("###","携带数据3:"+xiedai);

3.效果图:
安卓 广播(有序、粘性、EventBus)_第2张图片

粘性广播

//发送方

// An highlighted block
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sendMessage();
        sleepMessage();
    }

    public void sendMessage(){
        Intent intent = new Intent("com.bw.nianxing");
        intent.putExtra("message","粘性广播");
        sendStickyBroadcast(intent);
    }


    public void sleepMessage(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                    Intent intent = new Intent(MainActivity.this,OntherActivity.class);

                    startActivity(intent);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

//接收方

// An highlighted block
public class OntherActivity extends AppCompatActivity {

    MyRecevier myRecevier = new MyRecevier();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_onther);

        IntentFilter intentFilter = new IntentFilter("com.bw.nianxing");
        registerReceiver(myRecevier,intentFilter);

    }

    class MyRecevier extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("com.bw.nianxing")){
                String message = intent.getStringExtra("message");
                Log.e("###",message);
            }
        }
    }
}

EventBus

1.定义事件

// An highlighted block
public class MessageEvent{
    private String message;
    public  MessageEvent(String message){
        this.message=message;
    }
 
    public String getMessage() {
        return message;
    }
 
    public void setMessage(String message) {
        this.message = message;
    }
}

你可能感兴趣的:(安卓 广播(有序、粘性、EventBus))