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

TextMain.java

package lxy.litsoft;

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

更多相关文章

  1. 实现类似Android联系人搜索功能
  2. android 禁用解锁
  3. android使用两种方式注册receiver
  4. Android学习笔记(十九)
  5. Android(安卓)面试:常见问题总结
  6. android > 广播监听电话状态
  7. Android(安卓)实现开机自启动 Service
  8. Android(安卓)四大组件,五大存储,六大布局
  9. Android(安卓)系统服务的两种注册方式

随机推荐

  1. 安装Android时Could not find D:\Androi
  2. 补间动画--平移动画XML
  3. EditText使用小结
  4. Android开发布局系列: LinearLayout布局实
  5. Android--Selector、shape详解(整理)
  6. Android(安卓)神兵利器Dagger2使用详解(二
  7. Listview
  8. html5 开发android
  9. Android的应用程序结构分析:HelloActivity
  10. android 布局文件中xmlns:android="http: