一提起广播,我们首先想到的是收音机,当我们想要收听某个广播台时只需要将收音机的频率调至广播台所在的频率即可!而Android中的广播其实和收音机非常相似,不过它没有所谓的频率,它是由系统广播一个事件,然后由其他满足某一条件的程序接收并处理这个事件!! 要在Android中实现广播,首先我们要在Manifest.xml文件中配置一个<receiver/>标签,这个标签必须有一个android:name属性,值为继承自BroadcastReceiver类的接收器类!这个标签还有一个子标签为<intent-filter/>,这个标签很重要,是指定接收器需要接收哪种广播。另外,还有配置一个用户权限:<uses-permission/>,具体的值可以参考官方API文档。 另外一个比较重要的步骤是必须有一个类继承自BroadcastReceiver类,并复写onReceiver方法,在该方法中处理接收到广播后需要处理的事情! 下面来看一个具体的例子,有助于更好的理解广播机制是怎么一回事。 UI部分就不说了,Activity上就加了一个按钮,点击后发送广播。接收器接收到广播后在终端输出一句话。 首先看AndroidManifest.xml文件: <? xml version ="1.0" encoding ="utf-8" ?>
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package ="com.gufengxiachen.broadcast"
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 =".BroadCast"
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 =".MyBroadCastReceiver" >
< intent-filter >
< action android:name ="android.intent.action.EDIT" > </ action >
</ intent-filter >

</ receiver >
</ application >

< uses-permission android:name ="android.permission.RECEIVE_SMS" > </ uses-permission >
</ manifest >
下面是Activity: package com.gufengxiachen.broadcast;

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;
import android.widget.Button;

public class BroadCast extends Activity {
/** Called when the activity is first created. */
private Button bound = null;
private Button unbound = null;
private Button sendBroadCast = null;
private SecondBroadCastReceiver sbr = null;

public static final String SMS_ACTION= "android.provider.Telephony.SMS_RECEVIER";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bound = (Button)findViewById(R.id.bound);
unbound = (Button)findViewById(R.id.unbound);
sendBroadCast = (Button)findViewById(R.id.sendBroadCast);
bound.setOnClickListener( new BoundListener());
unbound.setOnClickListener( new UnboundListener());
sendBroadCast.setOnClickListener( new SendBroadCast());
}
class SendBroadCast implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub

Intent intent = new Intent(Intent.ACTION_EDIT);
BroadCast. this.sendBroadcast(intent);
}
}

class BoundListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sbr = new SecondBroadCastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(SMS_ACTION);
BroadCast. this.registerReceiver(sbr, filter);
}
}

class UnboundListener implements OnClickListener{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
BroadCast. this.unregisterReceiver(sbr);
}



}
}
最后是接收器类: package com.gufengxiachen.broadcast;

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

public class MyBroadCastReceiver extends BroadcastReceiver{
public MyBroadCastReceiver(){

}
@Override
public void onReceive(Context context, Intent intent) {

System.out.println( "message receiver");
}
}

更多相关文章

  1. Android实现自定义广播
  2. Android引入广播机制的用意。单线程模型Message、Handler、Messa
  3. Android(安卓)BroadcastReceiver(广播)实现消息发送
  4. Android和iPhone应用程序界面布局示例
  5. Android学习札记52:广播(Broadcast)基础篇
  6. android merge和include简单使用
  7. 国内目前最全面的介绍――Android中的BroadCastReceiver
  8. Android开机广播和关机广播
  9. 在代码中设置RelativeLayout布局中标签的android:layout_toLeftO

随机推荐

  1. 2020年5A PMP 经验分享
  2. 静态链表
  3. 栈和队列就是这么简单
  4. 解读容器的 2020:寻找云原生的下一站
  5. JVM中一个小知识点:深堆和浅堆的认识
  6. Linux网络基础概念
  7. 十道简单算法题
  8. HashMap的负载因子初始值为什么是0.75?这
  9. 格式化Curl返回的Json工具
  10. 大数据开发工程师完结