其实所谓的静态注册,动态注册,是指接收广播的时候,是静态注册接收还是动态注册接收,发送的时候不分静态,动态
以发送intent为例,
一共分4种情况,以每次注册两个Broadcast为例:
情况一,注册2个静态Broadcast
如果是静态注册的,接收的一定是某一个类继承BroadcastReceiver
2个java文件如下:
BroadcastActivity.java
代码
package com.broad.test1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;
//2个静态注册
public class BroadcastActivity extends Activity {
/** Called when the activity is first created. */
private Button button01;
private Button button02;
private static final String TAG = "BroadcastActivity";
private static final String INTENAL_ACTION_1 = "su1.bluetooth.true";
private static final String INTENAL_ACTION_2 = "su1.bluetooth.false";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button01 = (Button) findViewById(R.id.button01);
button02 = (Button) findViewById(R.id.button02);
button01.setOnClickListener(mybtnListener);
button02.setOnClickListener(mybtnListener);
}
private OnClickListener mybtnListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == (R.id.button01)) {
Intent intent = new Intent(INTENAL_ACTION_1);
// 也可以用mContext.sendBroadcast(intent),mContext.sendBroadcast可以在构造函数里面初始化,
// 有内部类的时候必须用???
sendBroadcast(intent);
} else if (v.getId() == (R.id.button02)) {
Intent intent = new Intent(INTENAL_ACTION_2);
sendBroadcast(intent);
} else {
Log.v(TAG, "not true not false");
}
}
};
}
BroadcastRecTest.java
代码
package com.broad.test1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BroadcastRecTest extends BroadcastReceiver {
private static final String TAG="BroadcastActivity";
private static final String INTENAL_ACTION_1="su1.bluetooth.true";
private static final String INTENAL_ACTION_2="su1.bluetooth.false";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(action.equals(INTENAL_ACTION_1)){
Log.d(TAG,"surx>>>>>>>>>>>>>>>>>>>>>>>>>>>>>su1.bluetooth.true");
}else if(action.equals(INTENAL_ACTION_2)){
Log.d(TAG,"surx>>>>>>>>>>>>>>>>>>>>>>>>>>>su1.bluetooth.false");
}else{
Log.d(TAG,"surx>>>>>>>>>>>>>>>>>>>>>>>>>>other type intent");
}
}
}
main.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="静态接收广播true"/>
<Button
android:id="@+id/button02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="静态接收广播false"/>
</LinearLayout>
AndroidManifest.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broad.test1"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BroadcastActivity"
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="BroadcastRecTest">
<intent-filter>
<action android:name="su1.bluetooth.true" />
<action android:name="su1.bluetooth.false" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
运行结果:
注意,静态注册的broadcast运行有点慢:
情况2,一个静态,一个动态注册Broadcast
BroadcastActivity.java
代码
package com.broad.test1;
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.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;
//1个动态,1个静态注册
public class BroadcastActivity extends Activity {
/** Called when the activity is first created. */
private Button button01;
private Button button02;
private static final String TAG="BroadcastActivity";
private static final String INTENAL_ACTION_1="su2.bluetooth.true";
private static final String INTENAL_ACTION_2="su2.bluetooth.false";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button01=(Button)findViewById(R.id.button01);
button02=(Button)findViewById(R.id.button02);
button01.setOnClickListener(mybtnListener);
button02.setOnClickListener(mybtnListener);
}
private OnClickListener mybtnListener=new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==(R.id.button01)){
Intent intent= new Intent(INTENAL_ACTION_1);
sendBroadcast(intent);
}else if(v.getId()==(R.id.button02)){
Intent intent= new Intent(INTENAL_ACTION_2);
sendBroadcast(intent);
}else{
Log.v(TAG,"not true not false");
}
}
};
// 接收动态注册广播的BroadcastReceiver
private BroadcastReceiver bcrIntenal2 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
//如果有多个动态注册的广播,要用if(action.equals()){}else if(action.equals()){}来判断是哪个
//String action = intent.getAction();
//这里只有一个,不用判断了。
Log.v(TAG,"surx>>>>>>>>>>>>>>>>>>>>>>>>>>>dong--su2.bluetooth.false");
}
};
//在onStart中动态注册广播,当然也可以在onCreate里面注册
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
//动态注册
IntentFilter intentFilter= new IntentFilter(INTENAL_ACTION_2);
//也可以用这种方法动态注册多个,说明我可以”接收“这么多的动态广播。
/* IntentFilter intentFilter= new IntentFilter();
intentFilter.addAction("action1");
intentFilter.addAction("action2");*/
registerReceiver(bcrIntenal2,intentFilter);
}
//在onStop中取消注册广播,如果在onCreate里面注册,那么在onDestroy里面取消注册。
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
unregisterReceiver(bcrIntenal2);
}
}
BroadcastRecTest.java
代码
package com.broad.test1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BroadcastRecTest extends BroadcastReceiver {
private static final String TAG="BroadcastActivity";
private static final String INTENAL_ACTION_1="su2.bluetooth.true";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(action.equals(INTENAL_ACTION_1)){
Log.d(TAG,"surx>>>>>>>>>>>>>>>>>>>>>>>>>>>>>su.bluetooth.true");
}else{
Log.d(TAG,"surx>>>>>>>>>>>>>>>>>>>>>>>>>>other type intent--jingtai");
}
}
}
main.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="静态接收广播true"/>
<Button
android:id="@+id/button02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="动态接收广播false"/>
</LinearLayout>
AndroidManifest.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broad.test1"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BroadcastActivity"
android:label="@string/app_name">
<intent-filt
发表评论
-
NetBeans快捷键
2011-03-20 13:13 705查找、搜索和替换 Ctrl-F ... -
细说util类,很全
2011-03-20 14:28 675线性表,链表,哈希表是常用的数据结构,在进行Java开发时,J ... -
线性表,链表,哈希表是常用的数据结构
2011-03-30 11:20 860线性表,链表,哈希表是常用的数据结构,在进行Java开发时,J ... -
Intent原理
2011-04-10 14:32 915最近在写程序,老是离 ... -
Broadcast学习
2011-04-10 14:40 696android Broadcast学习 其实所谓 ...
相关推荐
Service,Broadcast学习实例
android-broadcast的学习,新手上路,不喜勿喷
学习Android的四大组件之一Broadcast时的练习!
Broadcast 学习Demo
Android学习-broadcast实现音乐播放器
AE的Broadcast Colors.pdf 学习资料 复习资料 教学资源
Android系统中的广播(Broadcast)机制简要介绍和学习计划
这是Pro Android学习系列中Broadcast部分的例子源代码。相关学习笔记见:http://blog.csdn.net/flowingflying/article/details/6212512
学习广播,组播转换时,需要一个发广播的程序. 网上没找到,自己编写了一个. 这是用C++builder 6写的程序. 关于组播广播的网络拓扑,和Router配置, 去cisco找吧。 我在学习,有情趣的朋友请联络,一起进步。
android 广播基础学习资源
在Android开发中很少有activity service broadcast 单例的综合使用,我写的这个例子已经能运行了希望能和大家交流学习
主要为大家详细介绍了Android学习之Broadcast的简单使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
rabit是一个轻量级的库,提供Allreduce和Broadcast的容错接口。 它旨在支持分布式机器学习程序的轻松实现,其中许多自然属于Allreduce抽象。 rabit的目标是支持可移植,可伸缩且可靠的分布式机器学习程序。您也可以...
免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,...
这是一个关于broadcast电池电量显示的简单源码,供学习使用。
Android应用源码之broadcast.zip项目安卓应用源码下载Android应用源码之broadcast.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
Android broadcast电池电量显示源码.zip项目安卓应用源码下载Android broadcast电池电量显示源码.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
Android四大组件之一BroadcastReceiver实战demo,实现广播注册、发送、注销等功能,并给出实际开发过程中3种常见场景解决方案,1.... 是学习BroadcastReceiver实战与理论两者兼具的材料,下载即可运行学习。
android 源码学习. 资料部分来源于合法的互联网渠道收集和整理,供大家学习参考与交流。本人不对所涉及的版权问题或内容负法律责任。如有侵权,请通知本人删除。感谢CSDN官方提供大家交流的平台
android 源码学习. 资料部分来源于合法的互联网渠道收集和整理,供大家学习参考与交流。本人不对所涉及的版权问题或内容负法律责任。如有侵权,请通知本人删除。感谢CSDN官方提供大家交流的平台