android Broadcast学习
2016-03-23 14:15
453 查看
其实所谓的静态注册,动态注册,是指接收广播的时候,是静态注册接收还是动态注册接收,发送的时候不分静态,动态
以发送intent为例,
一共分4种情况,以每次注册两个Broadcast为例:
情况一,注册2个静态Broadcast
如果是静态注册的,接收的一定是某一个类继承BroadcastReceiver
2个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个静态注册
publicclass BroadcastActivity extends Activity
{
/** Called when the activity is first created. */
private Button button01;
private Button button02;
privatestaticfinal String
TAG ="BroadcastActivity";
privatestaticfinal String
INTENAL_ACTION_1 ="su1.bluetooth.true";
privatestaticfinal String
INTENAL_ACTION_2 ="su1.bluetooth.false";
@Override
publicvoid 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
publicvoid 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);
} elseif (v.getId() == (R.id.button02))
{
Intent intent =new Intent(INTENAL_ACTION_2);
sendBroadcast(intent);
} else {
Log.v(TAG, "not true not false");
}
}
};
}
代码
package com.broad.test1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
publicclass BroadcastRecTest extends BroadcastReceiver
{
privatestaticfinal String
TAG="BroadcastActivity";
privatestaticfinal String
INTENAL_ACTION_1="su1.bluetooth.true";
privatestaticfinal String
INTENAL_ACTION_2="su1.bluetooth.false";
@Override
publicvoid 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");
}elseif(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
代码
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个静态注册
publicclass BroadcastActivity extends Activity
{
/** Called when the activity is first created. */
private Button button01;
private Button button02;
privatestaticfinal String
TAG="BroadcastActivity";
privatestaticfinal String
INTENAL_ACTION_1="su2.bluetooth.true";
privatestaticfinal String
INTENAL_ACTION_2="su2.bluetooth.false";
@Override
publicvoid 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
publicvoid onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==(R.id.button01)){
Intent intent=new Intent(INTENAL_ACTION_1);
sendBroadcast(intent);
}elseif(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
publicvoid 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
protectedvoid 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
protectedvoid onStop() {
// TODO Auto-generated method stub
super.onStop();
unregisterReceiver(bcrIntenal2);
}
}
代码
package com.broad.test1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
publicclass BroadcastRecTest extends BroadcastReceiver
{
privatestaticfinal String
TAG="BroadcastActivity";
privatestaticfinal String
INTENAL_ACTION_1="su2.bluetooth.true";
@Override
publicvoid 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;
//两个动态注册
publicclass BroadcastActivity extends Activity
{
/** Called when the activity is first created. */
private Button button01;
private Button button02;
privatestaticfinal String
TAG="BroadcastActivity";
privatestaticfinal String
INTENAL_ACTION_1="su3.bluetooth.true";
privatestaticfinal String
INTENAL_ACTION_2="su3.bluetooth.false";
// private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
@Override
publicvoid 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
publicvoid onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==(R.id.button01)){
Intent intent=new Intent(INTENAL_ACTION_1);
sendBroadcast(intent);
}elseif(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
publicvoid 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");
}elseif(action.equals(INTENAL_ACTION_2)){
Log.v(TAG,"surx>>>>>>>>>>>>>>>>>>>>>>>>>>>dong--su3.bluetooth.false");
}
}
};
//在onStart中动态注册广播,当然也可以在onCreate里面注册
@Override
protectedvoid 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
protectedvoid 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的时候加权限
publicclass BroadcastActivity extends Activity
{
/** Called when the activity is first created. */
private Button button01;
private Button button02;
privatestaticfinal String
TAG="BroadcastActivity";
privatestaticfinal String
INTENAL_ACTION_1="su4.bluetooth.true";
privatestaticfinal String
INTENAL_ACTION_2="su4.bluetooth.false";
privatestaticfinal String
BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
@Override
publicvoid 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
publicvoid 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);
}elseif(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
publicvoid 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");
}elseif(action.equals(INTENAL_ACTION_2)){
Log.v(TAG,"surx>>>>>>>>>>>>>>>>>>>>>>>>>>>dong--su4.bluetooth.false");
}
}
};
//在onStart中动态注册广播,当然也可以在onCreate里面注册
@Override
protectedvoid 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
protectedvoid 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权限
运行结果:
两个Application间,发送广播,(以静态为例)
发送端:
接收端:
另一个application
1、Activity
2、BroadcastReceiver
3、AndroidManifest.xml
运行Application1,点击button,Application2广播接收端会收到这个广播,打印log信息(但不会显示BroadcastReceiverTest1Activity的界面),还是显示SenderTest1Activity的界面,
Log信息如下:
01-30 05:14:01.146: V/SenderTest1Activity(769): onCreate........
01-30 05:14:01.766: I/ActivityManager(90): Displayed com.sendbroadcast.test1/.SenderTest1Activity: +996ms
01-30 05:14:02.085: W/InputManagerService(90): Starting input on non-focused clientcom.android.internal.view.IInputMethodClient$Stub$Proxy@415548b0 (uid=10009
pid=182)
01-30 05:14:02.125: W/NetworkManagementSocketTagger(90): setKernelCountSet(10009, 0) failed with errno -2
01-30 05:14:02.735: V/SenderTest1Activity(769): @@@@@onclicksendbroadcast
01-30 05:14:02.735: V/SenderTest1Activity(769): onclicksendbroadcast
01-30 05:14:02.776: V/MyIntentReceiver(977): #####shoudao
接收端Application3,用动态方式接收。(把Application1的发送的action变成("su.broadcast.send2");)
先运行Application3,不能退出(back),否则就unregisterReceiver了,也就不能收到发送端发送的广播了。再运行Application1,点击button,发送广播,log信息如下:界面还是Application1的界面UI
01-30 07:13:14.918: V/SenderTest1Activity(651): @@@@@onclicksendbroadcast
01-30 07:13:14.918: V/SenderTest1Activity(651): onclicksendbroadcast
01-30 07:13:14.937: V/BroadcastReceiverTest2Activity(591): #####dongtaireceive
对于静态Application2接收广播来说,back之后也能接收到发送端发过来的广播!
注意:发送广播并不能调起另一个Activity!一直显示的都是发送广播的那个Activity的UI!!
总结:
以发送intent为例,发送广播都用sendBroadcast(intent);
静态注册接收的时候,只需要
(1)得到action,如:
(2)判断action类型,做出相应的动作。
动态注册接收的时候,需要:
(1) IntentFilter intentFilter= new IntentFilter();
(2)intentFilter.addAction(。。);可以添加多个
(3)注册接收,registerReceiver(BroadcastReceiver对象,intentFilter);别忘了在其他需要的地方取消注册接收!!!
(4)得到action,如:
以发送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个静态注册
publicclass BroadcastActivity extends Activity
{
/** Called when the activity is first created. */
private Button button01;
private Button button02;
privatestaticfinal String
TAG ="BroadcastActivity";
privatestaticfinal String
INTENAL_ACTION_1 ="su1.bluetooth.true";
privatestaticfinal String
INTENAL_ACTION_2 ="su1.bluetooth.false";
@Override
publicvoid 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
publicvoid 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);
} elseif (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;
publicclass BroadcastRecTest extends BroadcastReceiver
{
privatestaticfinal String
TAG="BroadcastActivity";
privatestaticfinal String
INTENAL_ACTION_1="su1.bluetooth.true";
privatestaticfinal String
INTENAL_ACTION_2="su1.bluetooth.false";
@Override
publicvoid 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");
}elseif(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个静态注册
publicclass BroadcastActivity extends Activity
{
/** Called when the activity is first created. */
private Button button01;
private Button button02;
privatestaticfinal String
TAG="BroadcastActivity";
privatestaticfinal String
INTENAL_ACTION_1="su2.bluetooth.true";
privatestaticfinal String
INTENAL_ACTION_2="su2.bluetooth.false";
@Override
publicvoid 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
publicvoid onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==(R.id.button01)){
Intent intent=new Intent(INTENAL_ACTION_1);
sendBroadcast(intent);
}elseif(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
publicvoid 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
protectedvoid 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
protectedvoid 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;
publicclass BroadcastRecTest extends BroadcastReceiver
{
privatestaticfinal String
TAG="BroadcastActivity";
privatestaticfinal String
INTENAL_ACTION_1="su2.bluetooth.true";
@Override
publicvoid 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;
//两个动态注册
publicclass BroadcastActivity extends Activity
{
/** Called when the activity is first created. */
private Button button01;
private Button button02;
privatestaticfinal String
TAG="BroadcastActivity";
privatestaticfinal String
INTENAL_ACTION_1="su3.bluetooth.true";
privatestaticfinal String
INTENAL_ACTION_2="su3.bluetooth.false";
// private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
@Override
publicvoid 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
publicvoid onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==(R.id.button01)){
Intent intent=new Intent(INTENAL_ACTION_1);
sendBroadcast(intent);
}elseif(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
publicvoid 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");
}elseif(action.equals(INTENAL_ACTION_2)){
Log.v(TAG,"surx>>>>>>>>>>>>>>>>>>>>>>>>>>>dong--su3.bluetooth.false");
}
}
};
//在onStart中动态注册广播,当然也可以在onCreate里面注册
@Override
protectedvoid 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
protectedvoid 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的时候加权限
publicclass BroadcastActivity extends Activity
{
/** Called when the activity is first created. */
private Button button01;
private Button button02;
privatestaticfinal String
TAG="BroadcastActivity";
privatestaticfinal String
INTENAL_ACTION_1="su4.bluetooth.true";
privatestaticfinal String
INTENAL_ACTION_2="su4.bluetooth.false";
privatestaticfinal String
BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
@Override
publicvoid 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
publicvoid 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);
}elseif(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
publicvoid 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");
}elseif(action.equals(INTENAL_ACTION_2)){
Log.v(TAG,"surx>>>>>>>>>>>>>>>>>>>>>>>>>>>dong--su4.bluetooth.false");
}
}
};
//在onStart中动态注册广播,当然也可以在onCreate里面注册
@Override
protectedvoid 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
protectedvoid 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权限
运行结果:
两个Application间,发送广播,(以静态为例)
发送端:
package com.sendbroadcast.test1; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; //import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.util.Log; public class SenderTest1Activity extends Activity { /** Called when the activity is first created. */ private Button btn1; private static String TAG = "SenderTest1Activity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn1 = (Button) findViewById(R.id.btn1); btn1.setText("susuSendBroadcast"); btn1.setOnClickListener(mylistener1); Log.v(TAG, "onCreate........"); } OnClickListener mylistener1 = new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Log.v(TAG, "@@@@@onclicksendbroadcast"); if (v.getId() == btn1.getId()) { Log.v(TAG, "onclicksendbroadcast"); Intent myIntent = new Intent(); myIntent.setAction("su.broadcast.send"); sendBroadcast(myIntent); } } }; }
接收端:
另一个application
1、Activity
package com.broadcast.receiver.test1; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class BroadcastReceiverTest1Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
2、BroadcastReceiver
package com.broadcast.receiver.test1; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class MyIntentReceiver extends BroadcastReceiver { @Override public void onReceive(Context arg0, Intent intent) { // TODO Auto-generated method stub String action =intent.getAction(); if(action.equals("su.broadcast.send")){ Log.v("MyIntentReceiver", "#####shoudao"); } } }
3、AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.broadcast.receiver.test1" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".BroadcastReceiverTest1Activity" 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=".MyIntentReceiver" android:label="wodeReceiver" > <intent-filter> <action android:name="su.broadcast.send" /> </intent-filter> </receiver> </application> </manifest>
运行Application1,点击button,Application2广播接收端会收到这个广播,打印log信息(但不会显示BroadcastReceiverTest1Activity的界面),还是显示SenderTest1Activity的界面,
Log信息如下:
01-30 05:14:01.146: V/SenderTest1Activity(769): onCreate........
01-30 05:14:01.766: I/ActivityManager(90): Displayed com.sendbroadcast.test1/.SenderTest1Activity: +996ms
01-30 05:14:02.085: W/InputManagerService(90): Starting input on non-focused clientcom.android.internal.view.IInputMethodClient$Stub$Proxy@415548b0 (uid=10009
pid=182)
01-30 05:14:02.125: W/NetworkManagementSocketTagger(90): setKernelCountSet(10009, 0) failed with errno -2
01-30 05:14:02.735: V/SenderTest1Activity(769): @@@@@onclicksendbroadcast
01-30 05:14:02.735: V/SenderTest1Activity(769): onclicksendbroadcast
01-30 05:14:02.776: V/MyIntentReceiver(977): #####shoudao
接收端Application3,用动态方式接收。(把Application1的发送的action变成("su.broadcast.send2");)
package com.myreceive.test2; 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.util.Log; public class BroadcastReceiverTest2Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } private BroadcastReceiver br= new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Log.v("BroadcastReceiverTest2Activity","#####dongtaireceive"); } }; @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); IntentFilter it = new IntentFilter(); it.addAction("su.broadcast.send2"); registerReceiver(br,it); Log.v("BroadcastReceiverTest2Activity","#####registerReceiver"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onStop(); unregisterReceiver(br); Log.v("BroadcastReceiverTest2Activity","#####unregisterReceiver"); } }
先运行Application3,不能退出(back),否则就unregisterReceiver了,也就不能收到发送端发送的广播了。再运行Application1,点击button,发送广播,log信息如下:界面还是Application1的界面UI
01-30 07:13:14.918: V/SenderTest1Activity(651): @@@@@onclicksendbroadcast
01-30 07:13:14.918: V/SenderTest1Activity(651): onclicksendbroadcast
01-30 07:13:14.937: V/BroadcastReceiverTest2Activity(591): #####dongtaireceive
对于静态Application2接收广播来说,back之后也能接收到发送端发过来的广播!
注意:发送广播并不能调起另一个Activity!一直显示的都是发送广播的那个Activity的UI!!
总结:
以发送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的shape
- DSS 流媒体服务器的搭建 Android端的读取
- Android解析自定义的链接的一些属性
- android 使用相机拍照,并存储到手机sd卡上,并利用系统录像录像并播放
- Android刷新转圈动画实现(一)
- Android 设置和不设置 android:targetSdkVersion的区别
- android加密:电子签名
- 如果在android中应用activemq
- android--day two 任务栈和生命周期
- Android Studio 出现 error: cannot find symbol variable abc_ic_ab_back_mtrl_am_alpha
- Android 短视频拍摄 第三方库SDK
- Android Activity生命周期与启动模式
- Android录屏命令、Android录Gif、Android录视频
- Android之ContentProvider总结
- Android WIFI密码查看器实例(在获取Root权限下查看系统文件)
- Android---颜色值的透明度
- Android适配器之BaseAdapter
- Android加速度传感器的使用:摇一摇功能的实现
- Android 实现形态各异的双向侧滑菜单 自定义控件来袭
- Android适配器之SimpleAdapter