先简单说一下思路:网络变化时系统会发出广播。所以我们监听这个广播,利用接口回调通知activity做相应的操作就好了。 思路 判断网络状态(写个工具类NetUtil) 写个类继承Broadcast
先简单说一下思路:网络变化时系统会发出广播。所以我们监听这个广播,利用接口回调通知activity做相应的操作就好了。
思路
- 判断网络状态(写个工具类NetUtil)
- 写个类继承BroadcastReceiver(不要忘记在清单文件中注册)
- 需要在清单文件中添加权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"> </uses-permission>
- 回调接口(NetEvevt)
- BaseActivity实现这个接口
直接上代码
NetUtil
import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; /** * Author: lenovo * Date: 2018/2/26 * email: 1271396448@qq.com */ public class NetUtil { /** * 没有连接网络 */ public static final int NETWORK_NONE = -1; /** * 移动网络 */ public static final int NETWORK_MOBILE = 0; /** * 无线网络 */ public static final int NETWORK_WIFI = 1; public static int getNetWorkState(Context context) { // 得到连接管理器对象 ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager .getActiveNetworkInfo(); if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) { if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) { return NETWORK_WIFI; } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) { return NETWORK_MOBILE; } } else { return NETWORK_NONE; } return NETWORK_NONE; } }
NetBroadReceiver
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; /** * Author: lenovo * Date: 2018/2/26 * email: 1271396448@qq.com */ public class NetBroadcastReceiver extends BroadcastReceiver { public NetEvevt evevt = BaseActivity.evevt; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub // 如果相等的话就说明网络状态发生了变化 if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { int netWorkState = NetUtil.getNetWorkState(context); // 接口回调传过去状态的类型 evevt.onNetChange(netWorkState); } } // 自定义接口 public interface NetEvevt { public void onNetChange(int netMobile); } }
BaseActivity
import android.os.Bundle; import android.support.v4.app.FragmentActivity; /** * Author: lenovo * Date: 2018/2/26 * email: 1271396448@qq.com */ abstract public class BaseActivity extends FragmentActivity implements NetBroadcastReceiver.NetEvevt { public static NetBroadcastReceiver.NetEvevt evevt; /** * 网络类型 */ private int netMobile; @Override protected void onCreate(Bundle arg0) { // TODO Auto-generated method stub super.onCreate(arg0); evevt = this; inspectNet(); } /** * 初始化时判断有没有网络 */ public boolean inspectNet() { this.netMobile = NetUtil.getNetWorkState(BaseActivity.this); return isNetConnect(); // if (netMobile == 1) { // System.out.println("inspectNet:连接wifi"); // } else if (netMobile == 0) { // System.out.println("inspectNet:连接移动数据"); // } else if (netMobile == -1) { // System.out.println("inspectNet:当前没有网络"); // // } } /** * 网络变化之后的类型 */ @Override public void onNetChange(int netMobile) { // TODO Auto-generated method stub this.netMobile = netMobile; isNetConnect(); } /** * 判断有无网络 。 * * @return true 有网, false 没有网络. */ public boolean isNetConnect() { if (netMobile == 1) { return true; } else if (netMobile == 0) { return true; } else if (netMobile == -1) { return false; } return false; } }
MainActivity
import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends BaseActivity { private TextView textView,txtView; ListView listView; //声明一个ListView对象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView= (TextView) findViewById(R.id.textView); txtView=(TextView)this.findViewById(R.id.txtView); //启动时判断网络状态 boolean netConnect = this.isNetConnect(); if (netConnect){ textView.setVisibility(View.GONE); txtView.setText("网络连接正常"); }else { textView.setVisibility(View.VISIBLE); txtView.setText("网络连接异常"); } } @Override public void onNetChange(int netMobile) { super.onNetChange(netMobile); //网络状态变化时的操作 if (netMobile==NetUtil.NETWORK_NONE){ textView.setVisibility(View.VISIBLE); txtView.setText("网络连接异常"); }else { textView.setVisibility(View.GONE); txtView.setText("网络连接正常"); } } }
记得在清单文件中注册
<receiver android:name="cn.broadcastreceiver.NetBroadcastReceiver" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。