注意了:android.net.conn.CONNECTIVITY_CHANGE 是用来监听网络变化的就是ConnectivityManager.CONNECTIVITY_ACTION
监听网络的状态:
1: 第一步就是注册权限,这个权限是,ACCESS_NETWORK_STATE,ACCESS_WIFI_STATE,INTERNET
如果是的话,就获取当前活动的连接,但是首先要获取当前的
ConnectivityManager cm = context.getSystemService(Service.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActivtNetWork();
int type = networkInfo.getType();
int state = networkInfo.getState();
switch(types){
case ConnectivityManager.TYPE_WIFI:
if(state = networkInfo.State.CONNECTED)
break;
....
监听网络的状态:
1: 第一步就是注册权限,这个权限是,ACCESS_NETWORK_STATE,ACCESS_WIFI_STATE,INTERNET
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.INTERNET"/>
2: 然后创建一个BroadcastReceiver,里面有一个onReceiver();
3: 在这个onReceiver里面来判断是否是这个ConnectivityManager.CONNECTIVITY_ACTION如果是的话,就获取当前活动的连接,但是首先要获取当前的
ConnectivityManager cm = context.getSystemService(Service.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActivtNetWork();
int type = networkInfo.getType();
int state = networkInfo.getState();
switch(types){
case ConnectivityManager.TYPE_WIFI:
if(state = networkInfo.State.CONNECTED)
break;
....
}
package tech.androidstudio.connectionbroadcastreceiverdemo; import android.annotation.TargetApi; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkInfo; import android.os.Build; import android.util.Log; /** * Created by Kodulf on 2016/2/29. */ public class ConnectionBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("NetworkReceiver", "收到广播消息" +intent ); String action = intent.getAction(); if(ConnectivityManager.CONNECTIVITY_ACTION.equals(action)){ ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Service.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if(activeNetwork!=null){ int subtype = activeNetwork.getSubtype(); NetworkInfo.State state = activeNetwork.getState(); int type = activeNetwork.getType(); switch (type){ case ConnectivityManager.TYPE_WIFI: if(state==NetworkInfo.State.CONNECTED){ Log.d("Kodulf","WIFI Connected++++++++++++++++"); }else{ Log.d("Kodulf","WIFI DisConnected+++++++++++++++++++"); } break; case ConnectivityManager.TYPE_MOBILE: if(state==NetworkInfo.State.CONNECTED){ Log.d("Kodulf","Mobile Connected+++++++++++++++++++"); }else{ Log.d("Kodulf","Mobile DisConnected+++++++++++++++++++"); } break; case ConnectivityManager.TYPE_BLUETOOTH: if(state==NetworkInfo.State.CONNECTED){ Log.d("Kodulf","BLUETOOTH Connected+++++++++++++++++++"); }else{ Log.d("Kodulf","BLUETOOTH DisConnected+++++++++++++++++++"); } break; } } } } }
4:然后在清单文件中添加recevier,这个有一个优先级为1000
<receiver android:name=".ConnectionBroadcastReceiver"> <intent-filter android:priority="1000"> <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> </intent-filter> </receiver>