广播接收器的两种注册方式:

1)动态注册:在代码中注册,创建一个IntentFilter(意图过滤器)对象,设置想要就收的广播,在onCreate()方法中通过调用registerReceiver()方法来注册广播接收器,在onDestroy()方法中通过调用unregisterReceiver()方法来注销广播接收器。

2)静态注册:在AndroidManifest.xml文件中注册,<Receiver>标签注册类,通过<IntentFilter>标签中的<Action>来过滤意图。静态注册的好处是,当程序关闭或者没有打开的时候,同样可以接收相关的广播。比如实现开机启动

面试:生命周期

1)广播接受者的生命周期是非常短暂的,在接收到广播的时候创建,onReceive()方法结束之后销毁

2)广播接受者中不要做一些耗时的工作,否则会弹出Application No Response错误对话框

3)最好也不要在广播接受者中创建子线程做耗时的工作,因为广播接受者被销毁后进程就成为了空进程,很容易被系统杀掉

4)耗时的较长的工作最好放在服务中完成

*无序广播

  1. <application
  2. android:allowBackup="true"
  3. android:icon="@mipmap/ic_launcher"
  4. android:label="@string/app_name"
  5. android:supportsRtl="true"
  6. android:theme="@style/AppTheme">
  7. <activity android:name=".MainActivity">
  8. <intent-filter>
  9. <action android:name="android.intent.action.MAIN" />
  10.  
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity>
  14. <receiver android:name=".MyBroadcastReceiver">
  15. <intent-filter>
  16. <action android:name="com.example.administrator.broadcastreceiver.MyBroadcastReceiver"></action>
  17. </intent-filter>
  18. </receiver>
  19. </application>
  1. public void send(View v){
  2. Intent intent = new Intent();
  3. intent.putExtra("name","王者");
  4. intent.putExtra("age",27);
  5. intent.setAction("com.example.administrator.broadcastreceiver.MyBroadcastReceiver");
  6. sendBroadcast(intent);
  7. }
  1. public class MyBroadcastReceiver extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. String name = intent.getStringExtra("name");
  5. int age = intent.getIntExtra("age",30);
  6. Log.i("Main",name+"--"+age);
  7. }
  8. }

*有序广播

  1. <application
  2. android:allowBackup="true"
  3. android:icon="@mipmap/ic_launcher"
  4. android:label="@string/app_name"
  5. android:supportsRtl="true"
  6. android:theme="@style/AppTheme">
  7. <activity android:name=".MainActivity">
  8. <intent-filter>
  9. <action android:name="android.intent.action.MAIN" />
  10.  
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity>
  14. <receiver android:name=".MyBroadcastReceiver1">
  15. <intent-filter android:priority="200">
  16. <action android:name="com.example.adminastrator.broadcastreceiver.MyBroadcastReceiver"></action>
  17. </intent-filter>
  18. </receiver>
  19. <receiver android:name=".MyBroadcastReceiver2">
  20. <intent-filter android:priority="100">
  21. <action android:name="com.example.adminastrator.broadcastreceiver.MyBroadcastReceiver"></action>
  22. </intent-filter>
  23. </receiver>
  24. </application>
  1. public void send(View v){
  2. Intent intent = new Intent();
  3. intent.putExtra("name","王者");
  4. intent.setAction("com.example.adminastrator.broadcastreceiver.MyBroadcastReceiver");
  5. sendOrderedBroadcast(intent,null);
  6. }
  1. public class MyBroadcastReceiver1 extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. String name = intent.getStringExtra("name");
  5. // abortBroadcast(); //拦截广播
  6. setResultData("哈哈"); //发送给优先级低的程序
  7. Log.i("Main",name+"----1");
  8. }
  9. }
  1. public class MyBroadcastReceiver2 extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. String name = intent.getStringExtra("name");
  5. String data = getResultData();
  6. Log.i("Main",name+"-"+data+"----2");
  7. }
  8. }

*自动回复短信

  1. <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
  2. <uses-permission android:name="android.permission.RECEIVE_SMS"/>
  3. <uses-permission android:name="android.permission.SEND_SMS"/>
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@drawable/ic_launcher"
  8. android:label="@string/app_name"
  9. android:theme="@style/AppTheme" >
  10. <activity
  11. android:name="com.example.broadcastReceiver.MainActivity"
  12. android:label="@string/app_name" >
  13. <intent-filter>
  14. <action android:name="android.intent.action.MAIN" />
  15.  
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. <receiver android:name="com.example.broadcastReceiver.MyReceiver">
  20. <intent-filter android:priority="500">
  21. <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
  22. </intent-filter>
  23. </receiver>
  24. </application>
  1. public class MyReceiver extends BroadcastReceiver {
  2.  
  3. @Override
  4. public void onReceive(Context context, Intent intent) {
  5. System.out.println("有短信来了");
  6. Bundle bundle = intent.getExtras();
  7. Object[] objs = (Object[]) bundle.get("pdus");
  8. for (Object obj : objs) {
  9. SmsMessage sms = SmsMessage.createFromPdu((byte[]) obj);
  10. String str = sms.getMessageBody();
  11. String phone = sms.getOriginatingAddress();
  12. Log.i("aaaaaaaaaaaa", phone+":"+str);
  13. if("114".equals(phone)){
  14. SmsManager manager = SmsManager.getDefault();
  15. manager.sendTextMessage(phone, null, "fuck you", null, null);
  16. }
  17. }
  18. }
  19. }

* 外拨电话添加前缀

  1. public class MyBroadcastReceiver extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. //收到外拨电话。
  5. String phone = getResultData();
  6. String name = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
  7. setResultData("10193"+phone);
  8. System.out.println("有外拨电话");
  9. Log.i("Main",phone);
  10. }
  11. }

* 监听网络状态

  1. <uses-permission android:name="android.permission.INTERNET"></uses-permission>
  2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
  3.  
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:supportsRtl="true"
  9. android:theme="@style/AppTheme">
  10. <activity android:name=".MainActivity">
  11. <intent-filter>
  12. <action android:name="android.intent.action.MAIN" />
  13.  
  14. <category android:name="android.intent.category.LAUNCHER" />
  15. </intent-filter>
  16. </activity>
  17. <receiver android:name=".MyReceiver">
  18. <intent-filter android:priority="100">
  19. <action android:name="android.net.conn.CONNECTIVITY_CHANGE"></action>
  20. </intent-filter>
  21. </receiver>
  22. </application>
  1. public class MyReceiver extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Service.CONNECTIVITY_SERVICE);
  5. NetworkInfo networkInfo = cm.getActiveNetworkInfo();
  6. if(networkInfo == null || !networkInfo.isConnected()){
  7. Log.i("Main","没有网络");
  8. }else{
  9. Log.i("Main","有网络");
  10. }
  11. }
  12. }

*监听网络状态(动态注册)

  1. private MyReceiver myReceiver;
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. myReceiver = new MyReceiver();
  7. }
  8.  
  9. @Override
  10. protected void onResume() {
  11. super.onResume();
  12. IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
  13. registerReceiver(myReceiver,filter);
  14. }
  15.  
  16. @Override
  17. protected void onDestroy() {
  18. super.onDestroy();
  19. unregisterReceiver(myReceiver);
  20. }
  21.  
  22. class MyReceiver extends BroadcastReceiver{
  23.  
  24. @Override
  25. public void onReceive(Context context, Intent intent) {
  26. ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Service.CONNECTIVITY_SERVICE);
  27. NetworkInfo networkInfo = manager.getActiveNetworkInfo();
  28. if(networkInfo == null||!networkInfo.isConnected()){
  29. Log.i("Main","没有网络");
  30. }else{
  31. Log.i("Main","有网络");
  32. }
  33. }
  34. }

* 开机自启动

  1. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
  2.  
  3. <application
  4. android:allowBackup="true"
  5. android:icon="@mipmap/ic_launcher"
  6. android:label="@string/app_name"
  7. android:supportsRtl="true"
  8. android:theme="@style/AppTheme">
  9. <activity android:name=".MainActivity">
  10. <intent-filter>
  11. <action android:name="android.intent.action.MAIN" />
  12.  
  13. <category android:name="android.intent.category.LAUNCHER" />
  14. </intent-filter>
  15. </activity>
  16. <receiver android:name=".MyBroadcastReceiver">
  17. <intent-filter >
  18. <action android:name="android.intent.action.BOOT_COMPLETED"></action>
  19. </intent-filter>
  20. </receiver>
  21. </application> 
  1. public class MyBroadcastReceiver extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. Intent intent1 = new Intent();
  5. intent1.setClass(context,MainActivity.class);
  6. intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  7. context.startActivity(intent1);
  8. }
  9. }

BroadcastReceiver广播接受者的更多相关文章

  1. Android BroadcastReceiver广播接受者

    静态注册 配置清单表注册:只要曾经注册过哪怕关闭也能调用  方式一:sendBroadCastReceive   广播的步骤:       发送  无序广播,普通广播       (1).发送方    ...

  2. BroadcastReceiver广播接受者简单使用

    1.注册BrocadcastReceiver <receiver android:name=".FirstReceiver" > <!-- 指定能够接收的广播类型 ...

  3. BroadcastRecevier广播接受者

    广播接收器的两种注册方式: 1)动态注册:在代码中注册,创建一个IntentFilter(意图过滤器)对象,设置想要就收的广播,在onCreate()方法中通过调用registerReceiver() ...

  4. Android四大组件之一:BroadCastReceiver(广播接收者)

    广播接受者是(BroadCastReceiver)是Android中的地大组件之一,之前学习了一些关于BroadCastReceiver方面的知识,今天回过头来发现已经快忘记的差不多了,毕竟现在是刚开 ...

  5. android84 广播接受者

    #广播接收者(广播接受者进程关闭了也能接收到广播,系统会在清单文件中找哪个广播接受者可以收到这条广播,然后去启动这个接受者的进程,找不到则广播发了就发了没人收到而已) * 现实中:电台要发布消息,通过 ...

  6. 在Service中使用广播接受者

    1.清单文件 <service android:name="com.example.callmethod.MyService"></service> 2.开 ...

  7. Android初级教程IP拨号器初识广播接受者

    需求:输入ip号码并且保存在本地,监听打电话广播,如果电话号码以0开头,则加上ip区号拨打. 首先定义一个页面布局: <LinearLayout xmlns:android="http ...

  8. Android 学习笔记 BroadcastReceiver广播...

    PS:不断提升自己,是件好事... 学习内容: 1.BroadcastReceiver的使用.. 2.通过BroadcastReceiver去启动Service... 1.BroadcastRecei ...

  9. Android的BroadcastReceiver 广播 短信拦截

    如何去理解BroadcastReceiver(广播)?其实可以这样想,首先我们要有一个发送广播的"媒体",在这个例子中,我们暂且用activity组件作为这个媒体,当然以后会用到s ...

随机推荐

  1. div 只显示两行超出部分隐藏

    ; -webkit-box-orient: vertical;line-height: 26px } <td rowspan="2" colspan="2" ...

  2. jQuery - ajaxUpLoad.js

    ajaxFileUpload是一个异步上传文件的jQuery插件 语法:$.ajaxFileUpload([options]) options参数说明: 参数 作用 url 上传处理程序地址 file ...

  3. mysql 案例 ~ pt-archiver 归档工具的使用

    一 简介:今天咱们来聊聊pt-archiver的使用 二 相关参数 相关参数1   --statistics 结束的时候给出统计信息:开始的时间点,结束的时间点,查询的行数,归档的行数,删除的行数,以 ...

  4. Dom4j向XML中指定位置添加、删除、修改节点——(五)

    需求: 在第一本书作者后面增加描述 <描述>好书</描述>  思路:获取书下面下的所有节点(一个list集合),在list集合指定位置添加一个元素(list.add(index ...

  5. RPC原理

    RPC同步调用流程:(异步另说) 1)服务消费方(Client)以本地的调用方式调用远程服务. 2)客户端代理(Client Stub)接收到调用后负责将方法.参数等组装成能够进行网络传输的消息体. ...

  6. SpringBoot整合SpringDataElasticSearch操作ES

    (1).添加starter依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...

  7. 腾讯云外网IP直通后,遇到网络问题

    通过内网机器,先重启网卡 service network restart cd /usr/local/etc ./modify_route.sh

  8. AT91RM9200---SMC简介

    1.前言 SMC(Static Memory Controller)Atmel 9200静态存储控制器的简称,它可以产生信号来控制外部静态存储和外设.SMC可通过编程寄存器来进行配置. 它有8路片选和 ...

  9. Windows CreateFont:创建自己的字体

    原文地址:http://blog.csdn.net/softn/article/details/51718347 前面无论是使用文本输出函数还是 static 控件,字体都是默认的,比较丑陋,我们完全 ...

  10. zabbix3.0对tcp连接数和状态的监控优化

    zabbix3.0对tcp连接数及状态的监控优化 之前对tcp的监控采用netstat命令,发现在服务器繁忙的时候效果不理想,这个命令占用大量的cpu有时候高达90%以上,可能会导致业务的不稳定,所以 ...