本地广播的发送

public class MainActivity extends ActionBarActivity {


LocalBroadcastManager local;
LocalReciver localReciver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //得到一个实例
        local = LocalBroadcastManager.getInstance(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.localbroadcast.Local_BROADCAST");
        localReciver= new LocalReciver();
        local.registerReceiver(localReciver, intentFilter);
        
        Button bt =(Button) findViewById(R.id.bt);
        bt.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("com.example.localbroadcast.Local_BROADCAST");
//发送本地广播
local.sendBroadcast(intent);
}
});
    }
    @Override
    protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    local.unregisterReceiver(localReciver);
    }
    
    class LocalReciver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Toast.makeText(MainActivity.this, "接收到了本地广播。。。。。。", Toast.LENGTH_LONG).show();
    }
    }

你可能感兴趣的:(本地广播的发送)