- 浏览: 261914 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
tonytony3:
THANK YOU
Android多级树形菜单的实现 -
haiyang08101:
deleteNumber = "421f481e-7 ...
java对xml文件做增删改查 -
求求你帮帮我:
怎么把我的空格也当成节点处理了?
java对xml文件做增删改查 -
求求你帮帮我:
deleteNumber = "421f481e-7 ...
java对xml文件做增删改查 -
lpj13579:
多谢分享,学习学习
高仿小米盒子UI效果实现
最近看了下关于广播方面的东西,加深了理解,也纠正了自己以前的不正确的看法。
关于广播有2中注册方式,一种是在代码中注册,也叫动态注册,另一种是在AndroidManifest里面注册,也叫静态注册。
不管在哪个里面注册,都需要发送广播sendBroadcast()。广播发送的是一个Intent,intent里面一般是action。
如果是在代码里面注册,一般在onResume()方法里面注册。
@Override protected void onResume() { // TODO Auto-generated method stub myReceiver = new MyReceiver(); IntentFilter filter = new IntentFilter(); //向过滤器中添加action filter.addAction("com.android.shang"); //注册广播 registerReceiver(myReceiver, filter); Intent intent = new Intent(); intent.setAction("com.android.xiang"); //发送广播 sendBroadcast(intent); super.onResume(); }
在onPause()方法里面要取消注册。
@Override protected void onPause() { // TODO Auto-generated method stub unregisterReceiver(myReceiver); super.onPause(); }
当然,也可以在xml文件中注册:
<receiver android:name=".MyBrocastReceiverTest"> <intent-filter> <action android:name="com.android.xiang"></action> </intent-filter> </receiver>
如果发送的广播的action已经注册了,那么在onReceive()方法中就会接收到发过来的广播,继而做出一些处理。
你可以写出一个内部类:
class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub //从这里的log中可以看到是否接受到了广播 System.out.println("intent = " + intent); } }
当然也可以单独写一个类:
package com.android.TestBrocastReceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyBrocastReceiverTest extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub System.out.println("intent = " + intent); } }
一般如果在代码里面注册,就把广播写在代码里面,在xml文件中注册就单独写一个类。
注册在代码里面:
package com.android.TestBrocastReceiver; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; public class TestBrocastReceiver extends Activity { MyReceiver myReceiver; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override protected void onPause() { // TODO Auto-generated method stub unregisterReceiver(myReceiver); super.onPause(); } @Override protected void onResume() { // TODO Auto-generated method stub myReceiver = new MyReceiver(); IntentFilter filter = new IntentFilter(); //向过滤器中添加action filter.addAction("com.android.shang"); //注册广播 registerReceiver(myReceiver, filter); Intent intent = new Intent(); intent.setAction("com.android.xiang"); //发送广播 sendBroadcast(intent); super.onResume(); } class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub //从这里的log中可以看到是否接受到了广播 System.out.println("intent = " + intent); } } }
注册在xml文件中:
在xml文件中注册,写上一个广播类,然后在activity中发送广播就可以了。
有时系统会发出一些广播,比如说时间的变化等,时间走一秒就会发一个广播,这时我们可以监听这个广播,然后在接收器中做处理就可以了,而不需要在发广播了。
评论
1 楼
warnerhit
2011-12-22
filter.addAction("com.android.shang");
intent.setAction("com.android.xiang");
很重要的两句没解释
intent.setAction("com.android.xiang");
很重要的两句没解释
发表评论
-
Android 三大图片加载框架比较
2016-11-30 10:31 10311.哪三大图片加载框架? 1) Pi ... -
仿微信小视屏拍摄的进度条/自定义的progressBar的样式。
2016-08-20 00:25 975今天有个需求,就是需要倒计时的进度条,样式参考微信小视屏拍摄 ... -
android 闹钟定时任务 AlarmManager
2016-06-02 13:41 1768直接上代码 package com.exampl ... -
android应用实现重启系统
2016-06-02 13:33 34211.在AndroidManifest.xml文件的manif ... -
android 每天定时提醒功能实现
2016-06-01 22:42 0android要实现定时的功 ... -
Java Calendar 类的时间操作
2016-06-01 21:02 616ava Calendar 类时间操作,这也许是创建日历和管理 ... -
android中fragment和activity之间相互通信
2014-11-13 16:25 1388在用到fragment的时候,老是会遇到一个问题,就是fra ... -
墨迹天气上下滑动viewpager
2014-11-04 17:15 667墨迹天气上下滑动viewpager -
Android杂谈--打开文件的Intent及使用
2014-10-25 13:03 596在写文件管理系统时会用到各种打开不同格式的文件的需求,由于A ... -
获取Android签名证书的sha1值
2014-08-23 23:14 622前言:今天开始研 ... -
java中Keytool的使用总结
2014-08-23 22:40 499J2SDK提供了keytool命令行工具,可以根据指定的参数 ... -
android获取versionName和versionCode
2014-07-08 18:02 687<TextView android:textSize= ... -
Android如何防止apk程序被反编译
2014-07-08 17:22 660作为Android应用开发者,不得不面对一个尴尬的局面,就是 ... -
在GridView中实现单选或多选功能
2014-06-26 13:08 769由于项目需要,要用到GridView实现多选功能,在网上搜 ... -
Android中Tween动画和Frame动画实例
2014-06-26 10:46 673Animation主要有两种动画模式:Tween动画和Fra ... -
android 禁用和开启四大组件的方法(setComponentEnabledSetting )
2014-06-19 15:26 0为什么要关闭组件? 在用到组件时,有时候我们可能暂时性的不使 ... -
android MD5校验码的生成与算法实现
2014-06-17 10:58 820在Java中,java.security.Mes ... -
android 自动检测版本升级
2014-06-15 14:10 349这里我用别的app代替了,简单省事,正常的话,你要对你的 ... -
layout左右平滑控制.
2014-04-30 09:41 563<?xml version="1.0&quo ... -
android让你的TabHost滑动起来
2014-04-22 22:13 774在Android应用中,一般TabActivity和若干个T ...
相关推荐
该文章清晰的说明了BroadCastReceiver的用法及其方法如何发送以及接收广播
BroadCastReceiver
介绍BroadcastReceiver使用的demo,具体的使用方法可以参看我的博客
android的broadcastReceiver的介绍。
BroadcastReceiver,两种注册方式
Android \BroadCastReceiver 最全的简介
BroadcastReceiver学习(01)
android BroadcastReceiver相关测试代码。我测试所用的。
一般来说,都是在一个Activity启动的时候使用这样的方法注册BroadcastReceiver,一旦接收到广播的事件,就可以在onReceive方法当中更新当前的这个Activity当中的控件。但是需要注意的是如果这个Activity不可见了,就...
android BroadcastReceiver 源码 学习
以获取电池电量为例对BroadcastReceiver和接口进行了简单讲解
Android学习之BroadcastReceiver总结
Android下BroadcastReceiver的简单示例。详见博客:http://www.cnblogs.com/plokmju/p/android_BroadcastReceiver.html
这是一个关于Android中四大组件之一 —— BroadcastReceiver,即广播的一个demo,里面涉及到BroadcastReceiver的两种注册方式,也简单的实现了广播,通过点击按钮发送广播,收到广播后打印日志。
演示了BroadcastReceiver的简单用法。
Android-BroadcastReceiver高级应用,详细参见博客:http://www.cnblogs.com/plokmju/p/android_SystemBroadcastReceriver.html
BroadcastReceiver应用详解 BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播。
这是一个国外的android手机应用源码实例,BroadcastReceiver这个应用我还没用过,不知道是干什么的,源码可为学习安卓开发的做些参考吧。
博客《BroadcastReceiver详解》对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/38710901