BroadcastReceiver的两种注册方式(静态注册和动态注册)android开发教程

静态注册就是在AndroidManifest.xml文件中定义,注册的广播接收器必须继承BroadReceiver

动态注册就是在程序中使用Context.registerReceiver注册。

发送广播事件:通过Context.sendBroadcast来发送,由Intent来传递注册时用到的Action。

接收广播:当发送的广播被接收器监听到后,会调用onReceive()方法,并将包含消息的Intent对象传回。

使用案例:

1、结构图:

\

 

2、Sample2-1_Activity.java代码如下:

package com.bn.ex2_1;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Sample2_1_Activity extends Activity {
	private Button sendStaticButton;	//发送自定义静态注册广播的按钮
	private Button sendDynamicButton;	//发送自定义动态注册广播的按钮
	private static final String STATICACTION = "com.bn.pp2.staticreceiver";	//静态广播的Action字符串
	private static final String DYNAMICACTION = "com.bn.pp2.dynamicreceiver";	//动态广播的Action字符串
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		sendStaticButton = (Button) findViewById(R.id.send_static);		//获取Button按钮引用
		sendDynamicButton = (Button) findViewById(R.id.send_dynamic);
		sendStaticButton.setOnClickListener(new DIYOnClickListener());	//为Button按钮添加监听器
		sendDynamicButton.setOnClickListener(new DIYOnClickListener());
	}
	class DIYOnClickListener implements OnClickListener{		//内部类OnClick监听器
		public void onClick(View v) {
			if(v.getId() == R.id.send_static){		// 发送自定义静态注册广播消息
				Intent intent = new Intent();
				intent.setAction(STATICACTION);		//设置Action
				intent.putExtra("msg", "接收静态注册广播成功!");	//添加附加信息
				sendBroadcast(intent);				//发送Intent
			}
			else if(v.getId() == R.id.send_dynamic){	// 发送自定义动态注册广播消息
				Intent intent = new Intent();
				intent.setAction(DYNAMICACTION);		//设置Action
				intent.putExtra("msg", "接收动态注册广播成功!");		//添加附加信息
				sendBroadcast(intent);					//发送Intent
	}}}
	@Override
	protected void onStart() {
		super.onStart();
		IntentFilter dynamic_filter = new IntentFilter();
		dynamic_filter.addAction(DYNAMICACTION);			//添加动态广播的Action
		registerReceiver(dynamicReceiver, dynamic_filter);	// 注册自定义动态广播消息
	}
	
	private BroadcastReceiver dynamicReceiver 				//动态广播的Receiver
	= new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if(intent.getAction().equals(DYNAMICACTION)){	//动作检测
				String msg = intent.getStringExtra("msg");
				Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}}};}

 

3、AndroidManifest.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      android:versionCode="1"
      android:versionName="1.0" package="com.bn.ex2_1">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".Sample2_1_Activity" 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=".StaticReceiver">
			<intent-filter>
				<action android:name="com.bn.pp2.staticreceiver" />
			</intent-filter>
		</receiver>
	</application>
</manifest>

 

4、StaticReceiver.java代码如下:

package com.bn.ex2_1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class StaticReceiver extends BroadcastReceiver {
	@Override		//静态广播接收器执行的方法
	public void onReceive(Context context, Intent intent) {
		String msg = intent.getStringExtra("msg");
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}}

 

5、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:textSize="24dip"
		android:gravity="center"
		android:text="BroadcastReceiver演示" />
	<Button 
		android:id="@+id/send_static" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" 
		android:text="发送自定义静态注册广播" />
	<Button 
		android:id="@+id/send_dynamic" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" 
		android:text="发送自定义动态注册广播" />
</LinearLayout>

6、运行截图:

\

 

\

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值