首页 > 代码库 > [Android]三种广播接收器1

[Android]三种广播接收器1

 

/************ 动态注册 ************/

ActivityMain.java 

package home.lee.broadcastreceiver; 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; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class TestMain extends Activity { BroadcastReceiver bcr;  //声明一个广播接收器 Button btSendMessage;   //声明一个按钮 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //实例化广播接收器对象  bcr = new BroadcastReceiver(){ public void onReceive(Context context, Intent intent) { Log.d("test", "^-^, Have received Massage!"); } }; //实例化按键对象并为其添加监听器 btSendMessage = (Button)findViewById(R.id.button01); btSendMessage.setOnClickListener(new ButtonListener()); } class ButtonListener implements OnClickListener{ public void onClick(View v) { //当点击按钮的时候,就发送广播 Intent intent = new Intent(); intent.setAction("ABC"); sendBroadcast(intent); } } protected void onStart() { super.onStart(); //注册广播接收器(动态注册) IntentFilter filter = new IntentFilter(); filter.addAction("ABC"); this.registerReceiver(bcr, filter); } protected void onStop() { super.onStop(); //取消注册广播接收器 this.unregisterReceiver(bcr); } }

 

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:text="SendMessage" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>


/************ 静态注册 ************/

AndroidManifest.xml 

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="lxy.litsoft" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".ActivityMain" 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="MyBroadcastReciever">   <intent-filter>   <action android:name="ABC"></action>   </intent-filter>   </receiver>   </application> </manifest>

 

TestMain.java

package lxy.litsoft; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class TestMain extends Activity { Button btSendMessage;   //声明一个按钮 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btSendMessage = (Button)findViewById(R.id.button01); btSendMessage.setOnClickListener(new ButtonListener()); } class ButtonListener implements OnClickListener{ public void onClick(View v) { //当点击按钮的时候,就发送广播 Intent intent = new Intent(); intent.setAction("ABC"); sendBroadcast(intent); } } }

 

MyBroadcastReciever.java 

package lxy.litsoft; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class MyBroadcastReciever extends BroadcastReceiver{ public void onReceive(Context context, Intent intent) { Log.d("test", "^-^, Have received Massage!"); } }


动态注册和静态注册一个BroadcastReceiver的区别

动态注册较静态注册灵活

实验证明:当静态注册一个BroadcastReceiver时,不论应用程序开启与否,可以接受对应的广播。

动态注册的时候,如果执行unregisterReceiver(); 方法取消注册,跟静态是一样的。但是如果执行该方法,当执行过以后,就不能接受广播了。