【Android】BroadCast广播机制应用与实例

如何编写广播接收器

第一步:需要继承BroadcastReceiver类,覆写其中的onReceive()方法. 

class MyBroadcastReceiver extends BroadcastReceiver {
	 //接收到广播会被自动调用	
	@Override
	public void onReceive (Context context, Intent intent) {
		//从Intent中获取action
		…your code here…
	}
}

第二步:定义好广播接收器还不行,必须向系统注册以便让其知道该接收器可以处理哪些广播事件。

常见方式是采用静态注册,修改MENIFEST.xml文件, 在<application></application>中加入receiver标签.

<application>
	<activity name=""/>
	<receiver android:name=".MyBroadcastReceiver">
		<!-- intent过滤器,指定可以匹配哪些intent, 一般需要定义action 可以是自定义的也可是系统的 --> 
		<intent-filter>
			<action android:name="com.app.bc.test"/>
		</intent-filter>
	</receiver>
</application>

第三步:此时我们可以发送一个广播事件出去,代码如下:

Intent intent = new Intent(“com.app.bc.test”);
sendBroadcast(intent);//发送广播事件

动态注册广播接收器

在某个Activity中,我们可以用代码来实现动态注册:
//生成一个BroadcastReceiver对象
SMSReceiver  smsReceiver = new SMSReceiver();
//生成一个IntentFilter对象
IntentFilter filter = new IntentFilter(); 		
filter.addAction(“android.provider.Telephony.SMS_RECEIVED”);
//将BroadcastReceiver对象注册到系统当中
//此处表示该接收器会处理短信事件
TestBC1Activity.this.registerReceiver(smsReceiver, filter); 

静态注册和动态注册的区别

1)静态注册:在AndroidManifest.xml注册,android不能自动销毁广播接收器,也就是说当应用程序关闭后,还是会接收广播。
2)动态注册:在代码中通过registerReceiver()手工注册.当程序关闭时,该接收器也会随之销毁。当然,也可手工调用unregisterReceiver()进行销毁。

操作小结

静态注册的步骤:

定义广播接收器,继承BroadcastReceiver类,覆写onReceive函数.
在xml文件中注册监听器,定义Intent-Filter中感兴趣的action操作.
使用sendBroadCast向系统发送对其感兴趣的广播接收器中.

动态注册的步骤:

SMSReceiver  smsReceiver = new SMSReceiver();
IntentFilter filter = new IntentFilter(); 		
filter.addAction(“android.provider.Telephony.SMS_RECEIVED”);
TestBC1Activity.this.registerReceiver(smsReceiver, filter); 

(无需在配置文件中注册接收器)

应用实例

package com.app.test02;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class BroadCastActivity1 extends Activity{
	Intent intent = new Intent();
	BroadCastTest1 bCastTest1 = new BroadCastTest1();
//	BroadCastTest11 bCastTest11 = new BroadCastTest11();
//	BroadCastTest111 bCastTest111 = new BroadCastTest111();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_bc1);
		//静态注册
		findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				intent.setAction("bc.test101");
				intent.putExtra("name", "静态的");
				sendBroadcast(intent);
//				sendOrderedBroadcast(intent, null);
			}
		});
		//动态注册
		findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				IntentFilter intentFilter = new IntentFilter();
				intentFilter.addAction("bc.test102");

				BroadCastActivity1.this.registerReceiver(bCastTest1, intentFilter);
				
				intent.setAction("bc.test102");
				intent.putExtra("name", "动态的");
				sendBroadcast(intent);
//				sendOrderedBroadcast(intent, null);
			}
		});

		findViewById(R.id.button3).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				unregisterReceiver(bCastTest1);
				finish();
			}
		});
	}
}

广播类
package com.app.test02;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class BroadCastTest1 extends BroadcastReceiver{
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		String name = intent.getStringExtra("name");
		
		Toast.makeText(context, "广播1:" + name, 1000).show();
		System.out.println(1);
	}
}

AndroidManifest.xml
        <receiver android:name=".BroadCastTest1">
            <intent-filter android:priority="3">
                <action android:name="bc.test101"/>
            </intent-filter>
        </receiver>

布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="#fff"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="静态注册广播" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="动态注册广播" />

        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="退出" />

    </LinearLayout>

</LinearLayout>



没有更多推荐了,返回首页