1. 程式人生 > >常用工具類:網路狀態判斷NetStateUtil()

常用工具類:網路狀態判斷NetStateUtil()

/**
 * 得到網路狀態的工具類
 * Created by lzh on 2017/10/16.
 */
public class NetStateUtil {

    /*
 * 判斷網路連線是否已開
 * true 已開啟  false 未開啟
 * */
    public static boolean isConn(Context context){
        boolean bisConnFlag=false;
        ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo network = conManager.getActiveNetworkInfo();
        if(network!=null){
            bisConnFlag=conManager.getActiveNetworkInfo().isAvailable();
        }
        return bisConnFlag;
    }

    /**
     * 當判斷當前手機沒有網路時選擇是否開啟網路設定
     * @param context
     */
    public static void showNoNetWorkDlg(final Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(R.mipmap.ic_launcher)         //
                .setTitle(R.string.app_name)            //
                .setMessage("當前無網路").setPositiveButton("設定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 跳轉到系統的網路設定介面
                Intent intent = null;
                // 先判斷當前系統版本
                if(android.os.Build.VERSION.SDK_INT > 10){  // 3.0以上
                    intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
                }else{
                    intent = new Intent();
                    intent.setClassName("com.android.settings", "com.android.settings.WirelessSettings");
                }
                context.startActivity(intent);

            }
        }).setNegativeButton("知道了", null).show();
    }
}

加許可權: android:name=”android.permission.ACCESS_NETWORK_STATE”

public class MainActivity extends AppCompatActivity {

    private ConnectivityManager connectivityManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initNetState();
    }

    private void initNetState() {
        //獲取網路狀態管理器
        connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
        //得到當前活躍的網路
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        NetworkInfo.State state = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        if (NetworkInfo.State.CONNECTED == state){
            Toast.makeText(this,"當前使用的是WiFi網路",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this,"當前使用的是行動網路",Toast.LENGTH_SHORT).show();
        }
        //得到行動網路狀態
        NetworkInfo.State stateMOBILE = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        if (NetworkInfo.State.CONNECTED == stateMOBILE){
            new AlertDialog.Builder(this)
                    .setTitle("設定")
                    .setIcon(R.mipmap.ic_launcher)
                    .setMessage("當前處於行動網路狀態,請在WiFi下使用")
                    .setPositiveButton("土豪訪問",new AlertDialog.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //編寫網路請求程式碼

                        }
                    })
                    .setNegativeButton("停止訪問",null)
                    .setNeutralButton("中立按鈕",null)
                    .show();
        }
        else{
            Toast.makeText(this,"無網路,請檢查",Toast.LENGTH_SHORT).show();
        }
    }
}