广播可以用来传递信息

public class MainActivity extends AppCompatActivity {
    private Button button1;
    private TextView textview1;
    private  MyBroadcast mBroadcast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button) findViewById(R.id.button1);
        textview1 = (TextView) findViewById(R.id.text1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
            }
        });

        mBroadcast = new MyBroadcast();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.skypan.up");
        LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcast,intentFilter);
    }

    private class MyBroadcast extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()){
                case "com.skypan.up":
                    textview1.setText("abc");
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcast);
    }
}

MyBroadcast为要注册的广播继承BroadcastReceiver类,并且重写onReceive的事件方法,然后把MyBroadcast进行实例化,

intentFilter通过后面的Filter就知道是一个过滤器,添加要发送广播的Action,使用LocalBroadcastManager能更好的不让其他应用接收到你广播

registerReceiver为注册广播.

public class Main2Activity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.skypan.up");
                LocalBroadcastManager.getInstance(Main2Activity.this).sendBroadcast(intent);
            }
        });
    }
}

发送广播后,onReceive事件方法里面会进行判断是不是我们的这个Action,如果是就执行里面的语句。