码迷,mamicode.com
首页 > 移动开发 > 详细

[Android]三种广播接收器1

时间:2014-06-08 07:41:58      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:android   c   class   code   java   a   

 

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

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(); 方法取消注册,跟静态是一样的。但是如果执行该方法,当执行过以后,就不能接受广播了。

 

[Android]三种广播接收器1,布布扣,bubuko.com

[Android]三种广播接收器1

标签:android   c   class   code   java   a   

原文地址:http://www.cnblogs.com/webapplee/p/3774022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!