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

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

时间: 2024-12-11 13:45:23

[Android]三种广播接收器1的相关文章

Android三种左右滑动效果 手势识别

Android三种左右滑动效果 手势识别(转) 手势识别 1.onCreate中添加GestureDetector mGestureDetector; //监听手势事件 mGestureDetector = new GestureDetector(this, onGestureListener); 2.//实现处理事件 OnGestureListener onGestureListener = new OnGestureListener() { //添加未实现的方法 }; 3.重写onTouch

android 三种弹出框之一poprpWindow

poprpWindow 在android的弹出框我目前了解到的是有三种:AlertDialog,poprpWindow,Activity伪弹框, AlertDialog太熟悉了,这里就不介绍了 就先看看poprpWindow API 给出的解释是: 意思就是一个展示view的弹出窗体,这个弹出窗体将会浮动在当前activity的最上层, 它和AlertDialog的区别是:在android中弹出框有两种方式:AlertDialog和PopupWindow,它们的不同点在于:      1.Ale

Android三种基本的加载网络图片方式(转)

Android三种基本的加载网络图片方式,包括普通加载网络方式.用ImageLoader加载图片.用Volley加载图片. 1. [代码]普通加载网络方式 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

Android三种实现自定义ProgressBar的方式介绍

一.通过动画实现 定义res/anim/loading.xml如下: View Row Code<?xml version="1.0" encoding="UTF-8"?><animation-list android:oneshot="false"xmlns:android="http://schemas.android.com/apk/res/android"><item android:du

android 三种网络通信接口及各个接口的代码示例

第一部分 Android网络基础 Android平台浏览器采用了WeBKit引擎,这款名为Chorme Lite的Web浏览器拥有强大扩展特性,每个开发者都以为编写自己的插件,使得浏览器的功能更加完善. 目前Android平台有3种网络接口. 第一种 java.net.*(标准java接口) 此接口提供与联网有关的类,包括流和数据包套接字.Internet协议.常见HTTP处理.如:创建URL以及URLConnection/HttpURLConnection对象.设置连接参数.连接服务器.向服务

android 三种定位方式

一共有三种定位方式,一种是GPS,一种是通过网络的方式,一种则是在基于基站的方式,但是,不管哪种方式,都需要开启网络或者GPS 首先添加权限 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 在C

Android 三种方式实现自定义圆形进度条ProgressBar

一.通过动画实现 定义res/anim/loading.xml如下: <?xml version="1.0" encoding="UTF-8"?> <animation-list android:oneshot="false" xmlns:android="http://schemas.android.com/apk/res/android"> <item android:duration=&qu

Android wifi状态三种广播

public class NetworkConnectChangedReceiver extends BroadcastReceiver{      @Override      public void onReceive(Context context, Intent intent) {      if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {//这个监听wifi的打开与关闭,与wifi的连接无关 

android三种加载图片方式

package smalt.music.utils; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; //载入图片的方法:3种 public class BitmapUntil { // 直接加载图片 public static Bitmap getBitmap(String path) { Bitmap bt