一、工程整体图

二、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. 【BOI2007】【BZOJ1176】Mokia

    1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MB Submit: 1059 Solved: 432 [Submit][St ...

  2. 关于WEB三层架构的思考

    1.MVC设计思想 MVC程序设计思想是眼下比較流行的WEB开发的模式,当中,M(model)是模型.即JavaBean,用来封装和保存数据:V(view)是视图,即JSP.用来显示内容:C(cont ...

  3. JQuery - 去除所有空格

    $('#submit').click(function () { //去除所有空格 String.prototype.NoSpace = function () { return this.repla ...

  4. Windows通过远程桌面访问Ubuntu

    关于Windows通过远程桌面访问Ubuntu 问题及目标 Window环境通过远程桌面访问Ubuntu Ubuntu机器端   1.  安装所需软件包   sudoapt-get install x ...

  5. C语言总结之---关键字

    我记得我开始学习C语言的时候,那时候还在读高中,我们老师就把C语言的关键字,全部写在黑板上,老师说我们下面的两节课的内容就是(把它给记下来) 你还记得标准C有多少个关键字吗? 第一:关键字描述 C99 ...

  6. 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters

    题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...

  7. wifi定位原理

    wifi定位和手机基站定位类别似,两者都需要收集wifi位置信息接入点. 其实WIFI奇妙,它靠的是侦測附近周围全部的无线网路基地台 (WiFi Access Point) 的 MAC Address ...

  8. 国际化之MessageFormat与占位符

    如果一个字符串文本中包含了多个与国际化相关的数据,可以使用MessageFormat类对这些数据进行批量处理. 例如: 在2016年1月9日的时候,一场台风导致了500间房屋的摧毁和¥1000000元 ...

  9. 如何让DbGrid支持鼠标滚轮滚动

    如何让DbGrid支持鼠标滚轮滚动 在主窗体上加一个ApplicationEvents控件(控件在Additional面板中), 在它的OnMessage事件中加入下述代码,一切搞定-! proced ...

  10. jeecg智能开发平台参与-2013年度中国优秀开源项目评比

    JEECG正在参与<2013年度中国十大优秀开源项目> 评比,如果大家觉得JEECG还不错, 请投出你宝贵的一票,给我们以支持吧!!! [目前排名第8位] https://code.csd ...