动态广播

package shuaxin.bwiei.com.guangbo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    final private String DongTai = "dongtai";
    private BroadcastReceiver br;

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


        br = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {


                Toast.makeText(context, "这是广播", Toast.LENGTH_SHORT).show();

            }
        };


        IntentFilter filter = new IntentFilter();
        filter.addAction(DongTai);

        //注册
        registerReceiver(br,filter);
        Intent in  = new Intent();
        in.setAction(DongTai);

        //发送广播
        sendBroadcast(in);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        unregisterReceiver(br);
    }
}

你可能感兴趣的:(动态广播)