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

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

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

面试:生命周期

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

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

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

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

*无序广播

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.example.administrator.broadcastreceiver.MyBroadcastReceiver"></action>
</intent-filter>
</receiver>
</application>
public void send(View v){
Intent intent = new Intent();
intent.putExtra("name","王者");
intent.putExtra("age",27);
intent.setAction("com.example.administrator.broadcastreceiver.MyBroadcastReceiver");
sendBroadcast(intent);
}
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age",30);
Log.i("Main",name+"--"+age);
}
}

*有序广播

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

*自动回复短信

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.broadcastReceiver.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.broadcastReceiver.MyReceiver">
<intent-filter android:priority="500">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
public class MyReceiver extends BroadcastReceiver {

    @Override
public void onReceive(Context context, Intent intent) {
System.out.println("有短信来了");
Bundle bundle = intent.getExtras();
Object[] objs = (Object[]) bundle.get("pdus");
for (Object obj : objs) {
SmsMessage sms = SmsMessage.createFromPdu((byte[]) obj);
String str = sms.getMessageBody();
String phone = sms.getOriginatingAddress();
Log.i("aaaaaaaaaaaa", phone+":"+str);
if("114".equals(phone)){
SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(phone, null, "fuck you", null, null);
}
}
}
}

* 外拨电话添加前缀

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

* 监听网络状态

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyReceiver">
<intent-filter android:priority="100">
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"></action>
</intent-filter>
</receiver>
</application>
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Service.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if(networkInfo == null || !networkInfo.isConnected()){
Log.i("Main","没有网络");
}else{
Log.i("Main","有网络");
}
}
}

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

private MyReceiver myReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myReceiver = new MyReceiver();
} @Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(myReceiver,filter);
} @Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver);
} class MyReceiver extends BroadcastReceiver{ @Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Service.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if(networkInfo == null||!networkInfo.isConnected()){
Log.i("Main","没有网络");
}else{
Log.i("Main","有网络");
}
}
}

* 开机自启动

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application> 
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent();
intent1.setClass(context,MainActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
}

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. luogu P3960 列队

    传送门 因为\(Splay\)可以\(O(logn)\)维护区间,所以直接对每一行维护第一个元素到倒数第二个元素的\(Splay\),最后一列维护一个\(Splay\),每次把选出来的点删掉,然后把那 ...

  2. python的特殊方法介绍

    __repr__.__str__ __len__.__getitem__.__setitem__.__delitem__.__contains__ __iter__.__reversed__.__ne ...

  3. 关于apache 开启 ssl https 支持 TLS1.2 的些事

    项目背景 需要搭建一个小程序的服务器,当然要使用https协议服务器windows service 2012 r2,后台语言是php,服务集成环境装的是appserv2.5 ,apache2.2证书申 ...

  4. JS 中对变量类型判断的几种方式

    文章整理搬运,出处不详,如有侵犯,请联系~   数据类型判断和数据类型转换代码工具 在 JS 中,有 5 种基本数据类型和 1 种复杂数据类型,基本数据类型有:Undefined, Null, Boo ...

  5. android 基础题

    1. Android的四大组件是哪些,它们的作用? (1).Activity:Activity是Android程序与用户交互的窗口,是Android构造块中最基本的一种,它需要为保持各界面的状态,做很 ...

  6. Shell高级编程学习笔记(基础篇)

    目录 1.shell脚本的执行方法  2.shell的变量类型  3.shell特殊变量 4.变量子串的常用操作  5.批量修改文件名实践   6.变量替换 7.在shell中计算字符串长度的方法  ...

  7. C++学习1-(C语言基础、VS快捷键)

    C语言基础复习 1.三码 正数: 3码合1 ,正数的反码/补码就是其本身 负数: 原码就是符号位加上真值的绝对值,即用第一位表示符号,其余位表示值 原码:11010101 负数的反码是在其原码的基础上 ...

  8. 六、regularized logisitic regssion练习(转载)

    转载链接:http://www.cnblogs.com/tornadomeet/archive/2013/03/17/2964858.html 在上一讲Deep learning:五(regulari ...

  9. Linux mmc framework2:基本组件之mmc

    1.前言 本文主要mmc组件的主要流程,在介绍的过程中,将详细说明和mmc相关的流程,涉及到其它组件的详细流程再在相关文章中说明. 2.主要数据结构和API TODO 3. 主要流程 3.1 mmc_ ...

  10. Servlet注释与部署描述符

    值得注意的是,部署描述符优先于注释.换句话说,部署描述符覆盖通过注释机制所规定的配置信息.Web 部署描述符的 3.0 版本在 web-app 元素上包含一种名为 metadata-complete ...