定义广播接收类
public class MyBroadcastReceiver extends BroadcastReceiver {    
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyBroadcastReceiver", "onReceive: ");
        Toast.makeText(context,"this is myReceiver",Toast.LENGTH_SHORT).show();
    }
}注册自定义广播<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.zoukeqing.helloworld">    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>        <receiver
            android:name=".MyBroadcastReceiver">
            <intent-filter>
                <action android:name="com.example.zoukeqing.helloworld.MY_BROADCAST"/>
            </intent-filter>
        </receiver>
    </application></manifest>点击按钮发送广播
public class MainActivity extends AppCompatActivity {    @Override
    protected void onCreate(Bundle savedInstanceState) {        Log.d("MainActivity", "onCreate: ");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);        Button btn = (Button)findViewById(R.id.btn_broadcast);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendBroadcast(new Intent("com.example.zoukeqing.helloworld.MY_BROADCAST"));
            }
        });    }
}