Android 利用BroadcastReceiver实时检测网络状态变化
来源:互联网 发布:淘宝上新抢拍技巧 编辑:程序博客网 时间:2024/05/17 03:29
一、定义ReceiveService类
当接收到的网络状态发生改变时通知Activity,需要一个Service,并且绑定该到Activity。import
java.util.Date;import java.util.Timer;import java.util.TimerTask;import android.app.Service;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class ReceiveMsgService extends Service { private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { Timer timer = new Timer(); timer.schedule(new NetworkTask(getApplicationContext()), new Date()); } } }; public interface GetConnectState { void GetState(boolean isConnected); // 网络状态改变之后,通过此接口的实例通知当前网络的状态,此接口在Activity中注入实例对象 } private GetConnectState onGetConnectState; public void setOnGetConnectState(GetConnectState onGetConnectState) { this.onGetConnectState = onGetConnectState; } private Binder binder = new MyBinder(); private boolean isConnected = true; @Override public IBinder onBind(Intent intent) { return binder; } @Override public void onCreate() {// 注册广播 IntentFilter mFilter = new IntentFilter(); mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); // 添加接收网络连接状态改变的Action registerReceiver(mReceiver, mFilter); } class NetworkTask extends TimerTask { private Context context; public NetworkTask(Context context) { this.context = context; } @Override public void run() { if (isNetworkConnected(context)) { isConnected = true; } else { isConnected = false; } if (onGetConnectState != null) { onGetConnectState.GetState(isConnected); // 通知网络状态改变 } } /* * 判断是否有网络连接 */ private boolean isNetworkConnected(Context context) { ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { return false; } else { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (NetworkInfo net : info) { if (net.getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; } } public class MyBinder extends Binder { public ReceiveMsgService getService() { return ReceiveMsgService.this; } } @Override public void onDestroy() { super.onDestroy(); unregisterReceiver(mReceiver); // 删除广播 }} 二、接下来在Activity中,绑定服务
注意:在TabActivity的子Activity中使用service时,bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE),需要改为:
this.getApplicationContext().bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE) ,才可正常使用。
public class MainActivity extends Activity { ReceiveMsgService receiveMsgService; private boolean conncetState = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.page_main); bind(); } private void bind() { Intent intent = new Intent(MainActivity.this, ReceiveMsgService.class); bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); } private ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { } @Override public void onServiceConnected(ComponentName name, IBinder service) { receiveMsgService = ((ReceiveMsgService.MyBinder) service) .getService(); receiveMsgService.setOnGetConnectState(new GetConnectState() { @Override public void GetState(boolean isConnected) { if (conncetState != isConnected) { conncetState = isConnected; if (conncetState) { handler.sendEmptyMessage(1); } else { handler.sendEmptyMessage(2); } } } }); } }; private void unbind() { if (receiveMsgService != null ) { unbindService(serviceConnection); } } Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 1: Toast.makeText(MainActivity.this, "网络已经连接" ,Toast.LENGTH_LONG).show(); break; case 2: Toast.makeText(MainActivity.this, "网络未连接" ,Toast.LENGTH_LONG).show(); break; default: break; } ; }; }; @Override protected void onDestroy() { super.onDestroy(); unbind(); } } 三、在Manifest中注册Serevice和相关权限
<usespermission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> //注册service<service android:enabled="true" android:name=".ReceiveMsgService" />
0 0