BroadcastReceiver
在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制。而BroadcastReceiver是对发送出来的 Broadcast进行过滤接受并响应的一类组件。BroadCastReceiver 源码位于: framework/base/core/java/android.content.BroadcastReceiver.java
发送Broadcast和使用BroadcastReceiver过滤接收的过程:
首先在需要发送信息的地方,把要发送的信息和用于过滤的信息(如Action、Category)装入一个Intent对象,然后通过调用 sendOrderBroadcast()或sendStickyBroadcast()方法,把 Intent对象以广播方式发送出去。
当Intent发送以后,所有已经注册的BroadcastReceiver会检查注册时的IntentFilter是否与发送的Intent相匹配,若匹配则就会调用BroadcastReceiver的onReceive()方法。所以当我们定义一个BroadcastReceiver的时候,都需要实现onReceive()方法。
注册BroadcastReceiver有两种方式:
静态注册:在AndroidManifest.xml中用标签生命注册,并在标签内用标签设置过滤器。
<span style="font-size:14px;"> <receiver android:name="com.example.networkstatereceiver.NetworkStateReceiver" ><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 19.5px; background-color: rgb(254, 254, 242);">//继承BroadcastReceiver,重写onReceiver方法</span>
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" /><span style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 19.5px; background-color: rgb(254, 254, 242);">//使用过滤器,接收指定action广播</span>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver></span>
动态注册:
<span style="font-size:14px;"> IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(String); //为BroadcastReceiver指定action,使之用于接收同action的广播
registerReceiver(BroadcastReceiver,intentFilter);
//一般:在onStart中注册,onStop中取消unregisterReceiver
//指定广播目标Action:Intent intent = new Intent(actionString);
//并且可通过Intent携带消息 :intent.putExtra("msg", "hi,我通过广播发送消息了");
//发送广播消息:Context.sendBroadcast(intent )</span>
demo实例
AndroidManifest.xml类
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.textview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light" >
<activity android:name="com.example.networkstatereceiver.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.networkstatereceiver.NetworkStateReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
java类
MainActivity程序
package com.example.networkstatereceiver;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent service = new Intent(this, NetworkStateReceiver.class);
startService(service);
}
}
NetworkStateReceiver广播
package com.example.networkstatereceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;
public class NetworkStateReceiver extends BroadcastReceiver {
private static final String TAG = "NetworkStateReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "network state changed.");
if (!isNetworkAvailable(context)) {
Toast.makeText(context, "网络不可用!", 0).show();
}else{
Toast.makeText(context, "网络可用!", 0).show();
}
}
/**
* 网络是否可用
*
* @param context
* @return
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] info = mgr.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}
}
参考: http://www.cnblogs.com/jico/articles/1838293.html
http://blog.csdn.net/liuhe688/article/details/6955668