`
Arthur.Chen
  • 浏览: 4509 次
  • 性别: Icon_minigender_1
  • 来自: 成都
最近访客 更多访客>>
社区版块
存档分类
最新评论

Broadcast学习

阅读更多

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

以发送intent为例,

一共分4种情况,以每次注册两个Broadcast为例:

情况一,注册2个静态Broadcast

如果是静态注册的,接收的一定是某一个类继承BroadcastReceiver

2java文件如下:

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics