说明:完成广播接收主要是onReceive的实现,由于是无向的,接收到的Intent可以是来自己任意一个发送者传递过来的,此处只需要关注对应action的Intent做出反应。
针对发送过来的数据,通过getExtra() 可以获取返回一个Bundle对象,然后通过Bundle的方法来获取最终的值,通过getAction返回收到的当前Intent行为是什么。
最后,还要在应用程序当中注册接收器能监听的action,此时需要在Androidmainifest.xml中设置receiver
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".IntentTestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space: normal;"> </activity>
<receiver android:name=".MyReceive">
<intent-filter>
<!-- 此处监听的action也就是onReceiver中能收到的广播消息,当然在intent-filter中可以添加多个action -->
<action android:name="om.android.IntetTest" />
</intent-filter>
</receiver>
</application></span></span>
</manifest>
更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11
