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     @Override
20     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             @Override
31             public void onClick(View v) {
32                 Intent intent = new Intent();
33                 intent.setAction(action);
34                 MainActivity.this.sendBroadcast(intent);//发送意图
35             }
36         });
37     }
38 
39     @Override
40     protected void onDestroy() {
41         unregisterReceiver(mMyboardcast);
42         super.onDestroy();
43     }
44 
45     private class Myboardcast extends BroadcastReceiver {
46 
47         @Override
48         public void onReceive(Context context, Intent intent) {
49             Log.e("Myboardcast", "收到广播!");
50         }
51 
52     }
53 }

 

posted on 2015-11-04 10:13  0代码狂人0  阅读(196)  评论(0编辑  收藏  举报