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

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);
}
}

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

  1. android84 广播接受者

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

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

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

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

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

  4. BroadcastReceiver广播接受者

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

  5. android 注册广播接受者

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 动态注册 静态注册 动态注册是 通过java代码,注册. 静态注册 是xml清单文件中 ...

  6. Android BroadcastReceiver广播接受者

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

  7. Android初级教程理论知识(第六章广播接受者)

    总体概述: 广播接收者 现实中:电台要发布消息,通过广播把消息广播出去,使用收音机,就可以收听广播,得知这条消息 Android中:系统在运行过程中,会产生很多事件,那么某些事件产生时,比如:电量改变 ...

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

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

  9. Android使用BroadCastRecevier广播实现接收短信,并利用Toast弹出显示内容

    在上一篇文章 Android简单实现BroadCastReceiver广播机制 中简单的实现了一个广播机制,这里利用BroadCarstRecevier实现一个接收短信并显示内容的案例,当然至于接收到 ...

随机推荐

  1. linux基本命令(2)-备份压缩命令

    一.tar命令 .解压文件 .tar.gz -C /opt (解压到/opt下) / /opt/tomcat (建立链接文件) 二.zip命令 // 1.把/home目录下面的mydata目录压缩为m ...

  2. Ueditor的两种定制方式

    引言 UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码... 官网:http://ueditor ...

  3. git 笔记- 概念

    本文参考书中内容 http://cnpmjs.org/ 镜像文件 下载插件的镜像 可参考fis 对于任何一个文件,在Git 内都只有三 种状态:已提交(committed),已修改(modified) ...

  4. 基础知识系列☞Abstract和Virtual→及相关知识

    转载地址→http://www.cnblogs.com/blsong/archive/2010/08/12/1798064.html 在C#的学习中,容易混淆virtual方法和abstract方法的 ...

  5. vsPhere安装虚拟sm

    1.在机器上单击右键 2.选择“编辑设置” 设备状态,选择打开电源时链接,数据存储ISO文件,选择镜象. 3.重启,进入安装界面. 4.

  6. js 判断所有节假日

    // JavaScript Document calendar = new Date(); month = calendar.getMonth(); date = calendar.getDate() ...

  7. Lucene4.3开发之分词器总结

    Lucene4.3开发之分词器总结 http://java.chinaitlab.com/tools/940011.html

  8. [ruby on rails] 跟我学之(2)HelloWorld

    1. 创建工程 rails new blog 2.查看下文件结构 tree 输出如下,请留意红圈中的部分. Gemfile, 用来管理应用程序的gems, 有点类似于python的包,有专门的网站来查 ...

  9. 【Spring】Spring系列4之Spring支持JDBC

    4.Spring支持JDBC 4.1.使用JdbcTemplate简化JDBC开发 也可以这么用(不推荐): 4.2.使用NamedParameterJdbcTemplate

  10. Search a 2D Matrix | & II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix, ret ...