Network judgment, simply call cached pictures without a network

Register in AndroidManifest first

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
package com.example.dell;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Created by dell on .
 */

public class ConntextUtils {
        public static boolean isConn(Context context){

            //1.得到网络判断的系统服务
            ConnectivityManager manager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            //2.得到网络信息类
            NetworkInfo info=manager.getActiveNetworkInfo();
            if(info!=null && info.isAvailable()){
                return true;
            }else{
                return false;
            }
        }
        public static  void openNetSettingDg(final Context context){
            AlertDialog.Builder builder=new AlertDialog.Builder(context);
            builder.setTitle("设置网络");
            builder.setMessage("是否要打开网络连接?");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //跳转到设置页面
                    Intent intent;
                    // 先判断当前系统版本
                    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);
                }
            });
            builder.setNegativeButton("取消",null);

            builder.show();
        }

}

Simple access to the image code block:

SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);

/**
     * 请求数据
     */
    private void initData() {

        new AsyncTask<String,Void,String>(){


            @Override
            protected String doInBackground(String... strings) {

                boolean flag = isNetworkAvailable(MainActivity.this);
                if (flag==true){

                    //有网
                    String json = NetWorkUtils.getStr();//请求的数据
                    //存
                    sp.edit().putString("s",json).commit();

                    return json;

                }else{
                    //没网
                    //取
                    String json = sp.getString("s",null);

                    return json;

                }

            }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325395866&siteId=291194637