1 import android.app.Activity; 2 import android.content.BroadcastReceiver; 3 import android.content.Context; 4 import android.content.Intent; 5 import android.content.IntentFilter; 6 import android.os.Bundle; 7 import android.provider.Browser.BookmarkColumns; 8 import android.util.Log; 9 import android.view.Menu;10 import android.view.MenuItem;11 import android.view.View;12 import android.view.View.OnClickListener;13 import android.widget.Button;14 15 public class MainActivity extends Activity {16 Myboardcast mMyboardcast;17 protected String action = "发送内容";18 19 @Override20 protected void onCreate(Bundle savedInstanceState) {21 super.onCreate(savedInstanceState);22 setContentView(R.layout.activity_main);23 Button button = (Button) findViewById(R.id.btn);24 mMyboardcast = new Myboardcast();//创建广播25 IntentFilter filter = new IntentFilter();26 filter.addAction(action);//过滤器27 registerReceiver(mMyboardcast, filter);28 button.setOnClickListener(new OnClickListener() {29 30 @Override31 public void onClick(View v) {32 Intent intent = new Intent();33 intent.setAction(action);34 MainActivity.this.sendBroadcast(intent);//发送意图35 }36 });37 }38 39 @Override40 protected void onDestroy() {41 unregisterReceiver(mMyboardcast);42 super.onDestroy();43 }44 45 private class Myboardcast extends BroadcastReceiver {46 47 @Override48 public void onReceive(Context context, Intent intent) {49 Log.e("Myboardcast", "收到广播!");50 }51 52 }53 }