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类型,做出相应的动作。
  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值