Android中判断网络是否连接(可用),不可用跳转设置网络连接

*本篇是判断网络是否可用(只供参考,不懂可以评论留言)


1)判断是否有网络连接 

代码如下:

public boolean isNetworkConnected(Context context) { 
if (context != null) { 
ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); 
if (mNetworkInfo != null) { 
return mNetworkInfo.isAvailable(); 


return false; 
}

2)判断WIFI网络是否可用  
代码如下:

public boolean isWifiConnected(Context context) { 
if (context != null) { 
ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo mWiFiNetworkInfo = mConnectivityManager 
.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
if (mWiFiNetworkInfo != null) { 
return mWiFiNetworkInfo.isAvailable(); 


return false; 
}

3)判断MOBILE网络是否可用
代码如下:

public boolean isMobileConnected(Context context) { 
if (context != null) { 
ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo mMobileNetworkInfo = mConnectivityManager 
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
if (mMobileNetworkInfo != null) { 
return mMobileNetworkInfo.isAvailable(); 


return false; 
}

4)获取当前网络连接的类型信息
代码如下:

public static int getConnectedType(Context context) { 
if (context != null) { 
ConnectivityManager mConnectivityManager = (ConnectivityManager) context 
.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); 
if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { 
return mNetworkInfo.getType(); 


return -1; 
}

*下面有一案例简单的判断了下网络,只供参考

public class MainActivity extends AppCompatActivity {

    private ListView listView;

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

        listView = (ListView) findViewById(R.id.list_view);

        //点击获取网络数据之前,判断一下网络是否可用...不可用,设置
        if (isNetConnected()){
            getAsylum();
        }else{
            //对话框
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

            builder.setTitle("警告");
            builder.setMessage("网络不可用,是否设置?");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //Settings.ACTION_WIRELESS_SETTINGS
                    //Settings.ACTION_WIFI_SETTINGS
                    Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
                    startActivity(intent);
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(MainActivity.this, "就这样,不设置网络", Toast.LENGTH_SHORT).show();
                }
            });
            builder.show();
        }
    }

    private void getAsylum() {
        new AsyncTask() {
            @Override
            protected String doInBackground(Void... voids) {
                String path = "路径";
                try {
                    URL url = new URL(path);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(5000);
                    int responseCode = connection.getResponseCode();
                    if (responseCode == 200){
                        InputStream inputStream = connection.getInputStream();
                        String json = StreamToString(inputStream, "utf-8");
                        return json;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);

                Gson gson = new Gson();
                UserBean userBean = gson.fromJson(s, UserBean.class);
                List newslist = userBean.getNewslist();
                MyAdapter myAdapter = new MyAdapter(MainActivity.this, newslist);
                listView.setAdapter(myAdapter);
            }
        }.execute();
    }

    private String StreamToString(InputStream inputStream, String s) {
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, s);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            StringBuilder builder = new StringBuilder();
            String s1 = "";
            while ((s1 = bufferedReader.readLine()) != null){
                builder.append(s1);
            }
            bufferedReader.close();
            return builder.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //判断网络是否可用
    public boolean isNetConnected() {
        //1.获取一个连接管理对象
        ConnectivityManager manager = (ConnectivityManager) MainActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
        2.获取网络连接的信息
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo != null){
            //返回值就代表网络是否可用
            return networkInfo.isAvailable();
        }
        return false;
    }
}

你可能感兴趣的:(数据请求,GET获取,判断网络,网络)