常驻型广播

常驻型广播,当你的应用程序关闭了,如果有广播信息来,你写的广播接收器同样的能接收到,它的注册方式就是在你应用程序的AndroidManifast.xml 中进行注册,这种注册方式通常又被称作静态注册。这种方式可以理解为通过清单文件注册的广播是交给操作系统去处理的。示例代码如下:

<!-- 订阅开机结束广播 -->
<receiver android:name=".receiver.BootCompleteReceiver" >
<intent-filter android:priority="1000" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

非常驻型广播

非常驻型广播,当应用程序结束了,广播自然就没有了,比如在 Activity 中的 onCreate 或者 onResume 中注册广播接收者,在 onDestory 中注销广播接收者。这样你的广播接收者就一个非常驻型的了,这种注册方式也叫动态注册。这种方式可以理解为通过代码注册的广播是和注册者关联在一起的。比如写一个监听 SDcard 状态的广播接收者:

package cn.sunzn.mosecurity.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Environment; public class SDcard extends Activity {
SdcardStateChanageReceiver sdcardStateReceiver; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sdcardStateReceiver = new SdcardStateChanageReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
filter.addAction(Intent.ACTION_MEDIA_EJECT);
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addDataScheme("file");
registerReceiver(sdcardStateReceiver, filter);
} protected void onDestroy() {
unregisterReceiver(sdcardStateReceiver);
} class SdcardStateChanageReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
checkSDCard();
} public void checkSDCard() {
String state = Environment.getExternalStorageState();
System.out.println(state);
if (state.equals(Environment.MEDIA_REMOVED) || state.equals(Environment.MEDIA_UNMOUNTED)) {
System.out.println("SDCard 已卸载!");
}
}
}
}

转 Android 编程下两种方式注册广播的区别的更多相关文章

  1. Android 编程下两种方式注册广播的区别

    常驻型广播 常驻型广播,当你的应用程序关闭了,如果有广播信息来,你写的广播接收器同样的能接收到,它的注册方式就是在你应用程序的AndroidManifast.xml 中进行注册,这种注册方式通常又被称 ...

  2. android环境下两种md5加密方式

    在平时开发过程中,MD5加密是一个比较常用的算法,最常见的使用场景就是在帐号注册时,用户输入的密码经md5加密后,传输至服务器保存起来.虽然md5加密经常用,但是md5的加密原理我还真说不上来,对md ...

  3. 【转】在Android Studio中下载Android SDK的两种方式(Android Studio3.0、windows)

    在Android Studio中下载Android SDK的两种方式(Android Studio3.0.windows) 方式一.设置HTTP Proxy1. 打开Settings2. 点击HTTP ...

  4. Android学习—下载Android SDK的两种方式

    在Android Studio中下载Android SDK的两种方式 Android studio下载地址:http://www.android-studio.org/ 方式一.设置HTTP Prox ...

  5. Android请求服务器的两种方式--post, get的区别

    android中用get和post方式向服务器提交请求_疯狂之桥_新浪博客http://blog.sina.com.cn/s/blog_a46817ff01017yxt.html Android提交数 ...

  6. 将Eclipse代码导入到Android Studio的两种方式

    转: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0104/2259.html 说到使用Android Studio,除了新建 ...

  7. Android视频播放的两种方式介绍

    1.在Android 中播放视频的方式有两种: 第一种方式是使用MediaPlayer 结合SurfaceView 来播放,通过MediaPlayer来控制视频的播放.暂停.进度等: 通过Surfac ...

  8. 在Android Studio中下载Android SDK的两种方式(Android Studio3.0、windows)

    方式一:网站下载:https://androidsdkmanager.azurewebsites.net/SDKPlatform 方式二.设置HTTP Proxy1. 打开Settings2. 点击H ...

  9. (android) SharedPreferences 两种方式的存储范围

    1 SharedPreferences settings =Activity.getPreferences(Activity.MODE_PRIVATE); 访问数据的范围为 当前的activity 2 ...

随机推荐

  1. SVG中image tag的高亮

    今天下午和晚上全部在实验室调试scs可视化,下午主要时间在调试字符云图的东西,不过感觉没有提升,还是不好看,囧~~ 把力道图中的线条给调整了,调细之后,好看了不少. 因为需要多个区域交互,也就是需要高 ...

  2. web开发路径问题

    1. web开发路径问题总结: http://www.cnblogs.com/tianguook/archive/2012/08/31/2665755.html 2. JSP/SERVLET 路径问题 ...

  3. jquery效果,多个div,点击任何一个div,那么这个div会切换文字,变换背景颜色,再次点击其他的div ,这个div会发生刚才的变化,之前点击的div的颜色会变回来

    首先css样式: /*原来的背景色*/ .bg { background:'red' } /*需要切换的背景色*/ .bg_click { background:'green' } JS: $('di ...

  4. android 布局页面文件出错故障排除Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V

    今天在看布局文件的时候出现 android 布局页面文件出错故障排除Exception raised during rendering: java.lang.System.arraycopy([CI[ ...

  5. pudn下载地址的规律

    A:http://download.pudn.com/downloads15/sourcecode/app/354278Cams.rar(随机数字6个)B:http://www.pudn.com/do ...

  6. Codeforces Round #350 (Div. 2)_D2 - Magic Powder - 2

    D2. Magic Powder - 2 time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. LeetCode OJ 202. Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  8. Python之路:爬虫之urllib库的基本使用和高级使用

    关于爬虫自己一直在看,所以时间太慢,这才第二更,有等不及的小伙伴可以慢慢的品尝了,在看下面的之前,建议先把上一章看一下.以下是关于python的Urllib的基础和高级用法. 1.如何扒下一个网站,用 ...

  9. Hadoop2.6.0 动态增加节点

    本文主要从基础准备,添加DataNode和添加NodeManager三个部分详细说明在Hadoop2.6.0环境下,如何动态新增节点到集群中. 基础准备 在基础准备部分,主要是设置hadoop运行的系 ...

  10. Hololens 开发环境配置

    安装 Hololens SDK 转自 Vangos Pterneas, 4 Apr 2016 CPOL    5.00 (1 vote) vote 1vote 2vote 3vote 4vote 5 ...