一箫一剑平生意,负尽狂名十五年。这篇文章主要讲述Android开始之 BroadcastReceiver相关的知识,希望能为你提供帮助。
【Android开始之 BroadcastReceiver】清单文件中定义:
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 { 2private NotificationManager manager; 3 4public MyBroadcastReceiver() { 5// TODO Auto-generated constructor stub 6} 7 8// 广播中不使用dialoog,,可以使用NotificationManager 9// 广播的生命周期就是onReceive 10@Override 11public void onReceive(Context context, Intent intent) { 12// TODO Auto-generated method stub 13manager = (NotificationManager) context 14.getSystemService(Context.NOTIFICATION_SERVICE); 15String name = intent.getStringExtra("name"); 16NotificationCompat.Builder builder = new NotificationCompat.Builder( 17context); 18builder.setTicker("广播来了"); 19builder.setSmallIcon(R.drawable.ic_launcher); 20builder.setContentTitle("没网了"); 21builder.setContentText("WiFi掉了" + name); 22manager.notify(1001, builder.build()); 23 24// Toast.makeText(context, "hello" + name, 0).show(); 25 26} 27 28 }
-----------------MainActivity ------------------------
1 public class MainActivity extends ActionBarActivity { 2private Button button; 3 4@Override 5protected void onCreate(Bundle savedInstanceState) { 6super.onCreate(savedInstanceState); 7setContentView(R.layout.fragment_main); 8button = (Button) findViewById(R.id.button1); 9button.setOnClickListener(new View.OnClickListener() { 10 11@Override 12public void onClick(View v) { 13// TODO Auto-generated method stub 14Intent intent=newIntent(); 15intent.setAction("abc"); 16intent.putExtra("name", "zy"); 17sendBroadcast(intent); 18} 19}); 20 21}
推荐阅读
- SpringBoot启动报错This application has no explicit mapping for /error.....
- android provider 使用解析
- 微软小娜app案例分析
- Android是如何判断APK是否不明来源的
- Applet 之 JAVA入门探索
- Android安全开发之启动私有组件漏洞浅谈
- 安卓开源项目周报0405
- 项目在App Store的展示信息
- Android - IPC之ContentProvider简介