码迷,mamicode.com
首页 > 移动开发 > 详细

Android开始之 BroadcastReceiver

时间:2017-04-06 20:10:46      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:list   color   pre   void   make   class   private   undle   out   

清单文件中定义:

 1 <receiver android:name=".MyBroadcastReceiver" >
 2             <intent-filter>
 3                 <action android:name="abc" />
 4             </intent-filter>
 5         </receiver>
 6         <receiver android:name=".MyBroadcastReceiver2" >
 7             <intent-filter>
 8                 <action android:name="abc" />
 9             </intent-filter>
10         </receiver>
11         <receiver android:name=".MyBroadcastReceiver3" >
12             <intent-filter>
13                 <action android:name="abc" />
14             </intent-filter>
15         </receiver>

--------------- MyBroadcastReceiver------------------------

 1 public class MyBroadcastReceiver extends BroadcastReceiver {
 2     private NotificationManager manager;
 3 
 4     public MyBroadcastReceiver() {
 5         // TODO Auto-generated constructor stub
 6     }
 7 
 8     // 广播中不使用dialoog,,可以使用NotificationManager
 9     // 广播的生命周期就是onReceive
10     @Override
11     public void onReceive(Context context, Intent intent) {
12         // TODO Auto-generated method stub
13         manager = (NotificationManager) context
14                 .getSystemService(Context.NOTIFICATION_SERVICE);
15         String name = intent.getStringExtra("name");
16         NotificationCompat.Builder builder = new NotificationCompat.Builder(
17                 context);
18         builder.setTicker("广播来了");
19         builder.setSmallIcon(R.drawable.ic_launcher);
20         builder.setContentTitle("没网了");
21         builder.setContentText("WiFi掉了" + name);
22         manager.notify(1001, builder.build());
23 
24         // Toast.makeText(context, "hello" + name, 0).show();
25 
26     }
27 
28 }

 

-----------------MainActivity ------------------------

 1 public class MainActivity extends ActionBarActivity {
 2     private Button button;
 3 
 4     @Override
 5     protected void onCreate(Bundle savedInstanceState) {
 6         super.onCreate(savedInstanceState);
 7         setContentView(R.layout.fragment_main);
 8         button = (Button) findViewById(R.id.button1);
 9         button.setOnClickListener(new View.OnClickListener() {
10             
11             @Override
12             public void onClick(View v) {
13                 // TODO Auto-generated method stub
14                 Intent intent=new  Intent();
15                 intent.setAction("abc");
16                 intent.putExtra("name", "zy");
17                 sendBroadcast(intent);
18             }
19         });
20 
21     }

 

Android开始之 BroadcastReceiver

标签:list   color   pre   void   make   class   private   undle   out   

原文地址:http://www.cnblogs.com/my334420/p/6674819.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!