一、工程整体图

二、activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:id="@+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="registerReceiver"
/> <Button
android:id="@+id/btn_unregister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unregisterReceiver"
/> <Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send"
/> </LinearLayout>

三、AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jltxgcy.broadcastreceiverdemo"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" /> <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<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>
<action android:name="com.jltxgcy.receiver" />
</intent-filter>
</receiver>
<service
android:name=".HelloIntentService">
</service>
</application> </manifest>

四、MainActivity.java

package com.jltxgcy.broadcastreceiverdemo;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener; public class MainActivity extends Activity {
public static final String ACTION="com.jltxgcy.receiver";
public BroadcastReceiver mBroadcastReceiver= new MyReceiver(); @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findViewById(R.id.btn_register).setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
/*IntentFilter filter = new IntentFilter();
filter.addAction(ACTION);
filter.setPriority(Integer.MAX_VALUE);
registerReceiver(mBroadcastReceiver, filter); */
}
}); findViewById(R.id.btn_unregister).setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// unregisterReceiver(mBroadcastReceiver); }
}); findViewById(R.id.btn_send).setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
sendBroadcast(new Intent(ACTION));
}
});
} @Override
protected void onStop() {
// unregisterReceiver(mBroadcastReceiver);
super.onStop();
} }

五、MyReceiver.java

package com.jltxgcy.broadcastreceiverdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log; public class MyReceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent arg1) {
Log.d("jltxgcy", "onReceiver");
Intent intent = new Intent(context, HelloIntentService.class);
context.startService(intent);
} }

六、HelloIntentService.java

package com.jltxgcy.broadcastreceiverdemo;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log; public class HelloIntentService extends IntentService {
public static final String TAG="jltxgcy"; public HelloIntentService() {
super("HelloIntentService");
// TODO Auto-generated constructor stub
} @Override
protected void onHandleIntent(Intent intent) {
long endTime = System.currentTimeMillis() + 5*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
Log.d(TAG, "onHandleIntent"+Thread.currentThread().getId());
} @Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
} }

七、详解

动态注册,不需要在AndroidManifest.xml中写上receiver。Activity退出后一定要关闭掉动态注册的receiver,否则会报告异常。

静态注册,需要在AndroidManifest.xml中写上receiver。即使Activity退出后,也在接受消息。

发送广播有两类,一个是系统发送的广播,一个是自己定义发送的广播。

在onReceiver,如果超过10s,那么就会产生ANR,最好的方法是开启一个IntentService,完成任务后自动关闭IntentService。

如果直接启动一个新的线程,那么无法控制线程的关闭。

BroadcastReceiver总结的更多相关文章

  1. Android中BroadcastReceiver的两种注册方式(静态和动态)详解

    今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...

  2. Android探索之BroadcastReceiver具体使用以及安全性探究

    前言: 最近的计划是学习一下iOS的NSNotificationCenter,突然想起来的Android的广播机制,所以还是觉得先对BroadcastReceiver来个全面的总结然后再去学习NSNo ...

  3. BroadcastReceiver注册、使用及其权限

    首先声明一个类,此类继承自BroadcastReceiver类,处理Android当中发出的广播事件: public class SMSReceiver extends BroadcastReceiv ...

  4. BroadcastReceiver详解

    详解 2014-08-20 19:42 13492人阅读 评论(8) 收藏 举报 分类: 5.andriod开发(148) 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+] ...

  5. Android四大组件之—— BroadcastReceiver的使用

    BroadcastReceiver又名广播接收者.既然它用于接收广播,那一定就有人负责发送. Android系统中的广播: 在现实生活中,我们都知道广播是什么,用来做什么.例如公园里的广播,主要通知游 ...

  6. Android四大核心组件之BroadCastReceiver

    实验内容 实现BroadCast发送和接受 通过BroadCast传递信息 动态注册和注销BroadCast 实验要求 实现BroadCast发送和接受 通过BroadCast传递信息 动态注册和注销 ...

  7. Android 广播 BroadcastReceiver

    Android 系统里定义了各种各样的广播,如电池的使用状态,电话的接收和短信的接收,开机启动都会产生一个广播.当然用户也可以自定义自己的广播. 既然说到广播,那么必定有一个广播发送者,以及广播接收器 ...

  8. Android中BroadcastReceiver广播

    BroadCastReceiver 简介 广播接收者( BroadcastReceiver )用于接收广播 Intent ,广播 Intent 的发送是通过调用 Context.sendBroadca ...

  9. BroadcastReceiver之(手动代码注册广播)屏幕锁屏、解锁监听、开机自启

    对于解锁和锁屏这种用的比较频繁action,谷歌做了限制,必须手动用代码注册 直接上代码:这是注册广播(手动代码注册广播接收者) public class MainActivity extends A ...

  10. BroadcastReceiver之有序广播

    有序广播可以按一定的优先级进行传播 首先进行发送广播 public void click(View v){ Intent intent = new Intent(); intent.setAction ...

随机推荐

  1. boost之program_options库,解析命令行参数、读取配置文件

    一.命令行解析 tprogram_options解析命令行参数示例代码: #include <iostream> using namespace std; #include <boo ...

  2. salon_百度百科

    salon_百度百科     salon    编辑    是法语Salon一字的译音,中文意即客厅,原指法国上层人物住宅中的豪华会客厅.从十七世纪,巴黎的名人(多半是名媛贵妇)常把客厅变成著名的社交 ...

  3. Android 讲述Help提示框

    Android 讲述Help提示框 XML/HTML代码 <stringname="help_dialog_text"> <i>Author:fonter. ...

  4. Cocos2d-x教程(30)-3.x版本号物理引擎的使用

    转载时请注明原文出处 : http://blog.csdn.net/u012945598/article/details/38417333 在Cocos2d-x 2.x的版本号中,开发人员能够直接使用 ...

  5. Swift - 使用MapKit显示地图,并在地图上做标记

    通过使用MapKit可以将地图嵌入到视图中,MapKit框架除了可以显示地图,还支持在地图上做标记. 1,通过mapType属性,可以设置地图的显示类型 MKMapType.Standard :标准地 ...

  6. java基本数据类型转换成byte[]数组

    import java.io.UnsupportedEncodingException;  public class ConToByte {      /**     * double转换byte   ...

  7. Eclipse更改默认工作目录的方法

    参考: Eclipse更改默认工作目录的方法:http://blog.163.com/take_make/blog/static/208212210201272611406227/ 用记事本打开&qu ...

  8. (摘录)ASP.NET提供文件下载函数(支持大文件、续传、速度限制、资源占用小)

    // 输出硬盘文件,提供下载 // 输入参数 _Request: Page.Request对象, _Response: Page.Response对象, _fileName: 下载文件名, _full ...

  9. [置顶] gis海量资源网盘提供VIP账号无广告高速下载 (更新更多资源)

    资源网盘下载地址:http://laoheitan.bego.cc/ 城通网盘 vip帐号共享 省去 烦人的 广告  多任务同时下载 独乐乐 不如众乐乐 好人 勿改密码. 获取到 vip下载连接后 请 ...

  10. (android高仿系列)今日头条 --新闻阅读器 (三) 完结 、总结 篇

    从写第一篇今日头条高仿系列开始,到现在已经过去了1个多月了,其实大体都做好了,就是迟迟没有放出来,因为我觉得,做这个东西也是有个过程的,我想把这个模仿中一步一步学习的过程,按照自己的思路写下来,在根据 ...