如何接受广播?
接受广播首先要编写一个广播接收器类,该类必须从BroadcastReceiver或其子类继承。
在BroadcastReceiver.onReceive(Context
context,Intent
intent)方法中编写处理广播的代码。但要注意,广播接收器必须在AndroidManifest.xml文件中注册,代码如下:
<receiver
android:name=".ShortMessageReceiver"
android:enable="true">
<intent-filter>
<!-- 指定可以接收的Broadcast Action-->
<action android:name=android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
如果同一个广播接收器处理多个广播,可以使用intent.getAction方法判断当前接受到的是哪一个广播,代码如下:
if("action1".equals(intent.getAction())){
......}
else if("action2".equals(intent.getAction())){
......}
原文:http://www.cnblogs.com/dazuihou/p/3585341.html