android Broadcast学习

其实所谓的静态注册,动态注册,是指接收广播的时候,是静态注册接收还是动态注册接收,发送的时候不分静态,动态

以发送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-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="su2.bluetooth.true" />
</intent-filter>
</receiver>

</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>

true是静态注册的,在Android.xml中注册的,false是动态注册的,在java代码中注册的

运行结果:

情况三、两个都是动态注册的,在同一个Activity里面既发送,又接收,(当然也可以在不同的java中,一个发送,另一个java接收,哪怕是不同的包)

动态注册不需要一个类继承BroadcastReceiver这个类了,直接new一个对象即可

代码

    
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;

//两个动态注册
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="su3.bluetooth.true";
private static final String INTENAL_ACTION_2="su3.bluetooth.false";
// private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
@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();
if(action.equals(INTENAL_ACTION_1)){
Log.v(TAG,"surx>>>>>>>>>>>>>>>>>>>>>>>>>>>dong--su3.bluetooth.true");
}else if(action.equals(INTENAL_ACTION_2)){
Log.v(TAG,"surx>>>>>>>>>>>>>>>>>>>>>>>>>>>dong--su3.bluetooth.false");
}

}
};
//在onStart中动态注册广播,当然也可以在onCreate里面注册
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
//动态注册两个,
IntentFilter intentFilter= new IntentFilter();
intentFilter.addAction(INTENAL_ACTION_1);
intentFilter.addAction(INTENAL_ACTION_2);
registerReceiver(bcrIntenal2,intentFilter);
}

//在onStop中取消注册广播,如果在onCreate里面注册,那么在onDestroy里面取消注册。
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
unregisterReceiver(bcrIntenal2);
}

}

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>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>

运行结果:

情况4,sendBroadcast的时候加权限,以2个都动态注册为例

代码

    
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;

//两个动态注册,sendBroadcast的时候加权限
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="su4.bluetooth.true";
private static final String INTENAL_ACTION_2="su4.bluetooth.false";
private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
@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,BLUETOOTH_PERM);
}else if(v.getId()==(R.id.button02)){
Intent intent= new Intent(INTENAL_ACTION_2);
sendBroadcast(intent,BLUETOOTH_PERM);
}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();
if(action.equals(INTENAL_ACTION_1)){
Log.v(TAG,"surx>>>>>>>>>>>>>>>>>>>>>>>>>>>dong--su4.bluetooth.true");
}else if(action.equals(INTENAL_ACTION_2)){
Log.v(TAG,"surx>>>>>>>>>>>>>>>>>>>>>>>>>>>dong--su4.bluetooth.false");
}

}
};
//在onStart中动态注册广播,当然也可以在onCreate里面注册
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
//动态注册两个,说明我可以”接收“2个这样Action的动态广播
IntentFilter intentFilter= new IntentFilter();
intentFilter.addAction(INTENAL_ACTION_1);
intentFilter.addAction(INTENAL_ACTION_2);
registerReceiver(bcrIntenal2,intentFilter);
}

//在onStop中取消注册广播,如果在onCreate里面注册,那么在onDestroy里面取消注册。
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
unregisterReceiver(bcrIntenal2);
}

}

发送的时候加了蓝牙权限,

若在使用sendBroadcast()的方法是指定了接收权限,则只有接收方的包内--在AndroidManifest.xml中用<uses- permission>标签声明了拥有此权限的BroascastReceiver才会有可能接收到发送来的Broadcast。

同样,若在注册BroadcastReceiver时指定了可接收的Broadcast的权限,则只有在接收方的包内的AndroidManifest.xml中 用<uses-permission>标签声明了,拥有此权限的Context对象所发送的Broadcast才能被这个 BroadcastReceiver所接收。

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>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
</manifest>

注意上面加的蓝牙permission权限

运行结果:

总结:

以发送intent为例,发送广播都用sendBroadcast(intent);

静态注册接收的时候,只需要

(1)得到action,如:

String action = intent.getAction();

(2)判断action类型,做出相应的动作。

动态注册接收的时候,需要:

(1) IntentFilter intentFilter= new IntentFilter();

(2)intentFilter.addAction(。。);可以添加多个

(3)注册接收,registerReceiver(BroadcastReceiver对象,intentFilter);别忘了在其他需要的地方取消注册接收!!!

(4)得到action,如:

String action = intent.getAction();(5)判断action类型,做出相应的动作。

android Broadcast学习相关推荐

  1. android fragment学习6--FragmentTabHost底部布局

    我个人觉得这个与fragment4 5是一样的,但是之前有三个项目中就是这样用的,没有出现问题,有必要拿出来分享下. 我的博客 android fragment学习5–fragment扩展 TabLa ...

  2. Android SurfaceFlinger 学习之路(五)----VSync 工作原理

    原址 VSync信号的科普我们上一篇已经介绍过了,这篇我们要分析在SurfaceFlinger中的作用.(愈发觉得做笔记对自己记忆模块巩固有很多帮助,整理文章不一定是用来给别人看的,但一定是为加强自己 ...

  3. android开发学习大体思路

    android开发学习: android学习的前提是java基础.如果你没有好的java基础,那就赶紧补充,我在这里不做介绍. android是基于linux的,如果你要做底层的东西,可以买一些关于l ...

  4. java/android 设计模式学习笔记(1)--- 单例模式

    前段时间公司一些同事在讨论单例模式(我是最渣的一个,都插不上嘴 T__T ),这个模式使用的频率很高,也可能是很多人最熟悉的设计模式,当然单例模式也算是最简单的设计模式之一吧,简单归简单,但是在实际使 ...

  5. Android Animation学习(五) ApiDemos解析:容器布局动画 LayoutTransition

    Android Animation学习(五) ApiDemos解析:容器布局动画 LayoutTransition Property animation系统还提供了对ViewGroup中的View改变 ...

  6. android Fragment 学习资料推荐

    为什么80%的码农都做不了架构师?>>>    android   Fragment 学习资料推荐:android大神 郭霖 http://blog.csdn.net/guolin_ ...

  7. android service 学习(上)

    转载自:http://www.cnblogs.com/allin/archive/2010/05/15/1736458.html Service是android 系统中的一种组件,它跟Activity ...

  8. android service 学习(下)

    android service 学习(下) 通常每个应用程序都在它自己的进程内运行,但有时需要在进程间传递对象,你可以通过应用程序UI的方式写个运行在一个不同的进程中的service.在android ...

  9. Android:学习AIDL,这一篇文章就够了(下)

    前言 上一篇博文介绍了关于AIDL是什么,为什么我们需要AIDL,AIDL的语法以及如何使用AIDL等方面的知识,这一篇博文将顺着上一篇的思路往下走,接着介绍关于AIDL的一些更加深入的知识.强烈建议 ...

最新文章

  1. 解密Kernel:为什么适用任何机器学习算法?
  2. redis单机版安装
  3. java操作字符串的工具类StringUtil
  4. Strange Birthday Party CodeForces - 1471C
  5. 【HDU - 5886】Tower Defence(树的直径,思维,dp)
  6. linux服务器规格查看
  7. 微信分享功能问题-描述内容换行,导致js加载失败
  8. 一年多 Let’s Encrypt 的 SSL 证书使用有感
  9. 影响世界的77部文学名著
  10. 高通源代码 Ubuntu14.04下载编译Android(1)
  11. 数据结构名词解释以及简答
  12. 学习的爬虫一点小感悟附上爬取淘宝信息的教程
  13. 集合论—关系的自反、对称和传递闭包
  14. Google Chrome 常用插件清单
  15. Cisco配置DHCP中继代理
  16. 软件研发模型和软件测试模型
  17. 工业机器人应用编程考核设备
  18. 进程创建的优化设计(上)
  19. select函数详细用法解析
  20. 荣耀8一下显示无服务器,买到荣耀手机后,不打开这七个功能你就亏了!

热门文章

  1. 「镁客早报」特斯拉标准版Model3正式上市,售价3.5万美元;百度被爆搜索小学和幼儿园会导向色情网站...
  2. 互联网企业如何有效落地SDL
  3. zoj 1010 Area【线段相交问题】
  4. DevOps是什么鬼?
  5. 为什么要把服务器托管在交易所机房?
  6. 鸿蒙系统电动车,鸿蒙系统被刷屏,新日电动车在其中是什么角色?
  7. 渗透测试常用术语总结
  8. 为什么共享充电宝能赚钱,共享单车不行?
  9. 20款 密码破解工具
  10. 不同粒径大小的金纳米粒子|球形金纳米粒子|单分散金纳米颗粒Glucose modified Gold Nanoparticles(粒径5nm)