在移动网络状态下,选择加载图片模式

这是自定义的一个方法,来根据你选择的加载模式选择不同的接口,三个网站接口只是模拟
public class ImageMode {
    public static final String SP_NAME = "SP_IMAGE_MODE_NAME";
    public static final String SP_KEY = "SP_KEY";


    public static final String BASE_BIG_URL = "www.baidu.com";
    public static final String BASE_SIMALL_URL = "www.sina.com";
    public static final String BASE_NO_URL = "www.NO.com";

    public static String BASE_URL = BASE_BIG_URL;
    private static boolean isMobile = false;
    private static ImageMode imageMode;

    //单利模式
    public static ImageMode getInstance() {
        if (imageMode == null) {
            synchronized (ImageMode.class) {
                if (imageMode == null)
                    imageMode = new ImageMode();
            }
        }
        return imageMode;
    }

    public String setBASEURL() {
        //如果是在移动网络状态,才进行设置
        if (isMobile) {
            int mode = MyAppLication.getMyApplication().getSharedPreferences(ImageMode.SP_NAME,
                    Context.MODE_PRIVATE).getInt(ImageMode.SP_KEY, 0);
            switch (mode) {
                case 0:
                    BASE_URL = BASE_BIG_URL;
                    break;
                case 1:
                    BASE_URL = BASE_SIMALL_URL;
                    break;
                case 2:
                    BASE_URL = BASE_NO_URL;
                    break;
            }
        } else {
            BASE_URL = BASE_BIG_URL;
        }
        return BASE_URL;
    }


    public static void ChangeMobile(boolean b) {
        isMobile = b;
    }
}
这里是主方法中处理
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private boolean isMobile = true;//判断是否是在移动网络状态下

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.setImageMode).setOnClickListener(this);
        findViewById(R.id.getImageMode).setOnClickListener(this);
        //注册网络监听
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(new MyBroadCasereceiver(), filter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setImageMode://设置加载模式
                int mode = getSharedPreferences(ImageMode.SP_NAME, MODE_PRIVATE).getInt
                        (ImageMode.SP_KEY, 0);
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("非wifi网络流量");
                String[] strings = {"最佳效果(下载大图)", "较省流量(智能下图)", "极省流量(不下载图)"};
                builder.setSingleChoiceItems(strings, mode, new
                        DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                getSharedPreferences(ImageMode.SP_NAME, MODE_PRIVATE).edit()
                                        .putInt(ImageMode.SP_KEY, which).commit();
                                //关闭弹窗
                                dialog.dismiss();
                            }
                        });

                builder.setNegativeButton("取消", null);
                builder.create().show();
                break;
            case R.id.getImageMode://查看当前模式
                Toast.makeText(this, "当前模式" + ImageMode.getInstance().setBASEURL(), Toast.LENGTH_SHORT).show();
                break;

        }
    }

    class MyBroadCasereceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
                ConnectivityManager manager = (ConnectivityManager) getSystemService(Context
                        .CONNECTIVITY_SERVICE);
                NetworkInfo info = manager.getActiveNetworkInfo();
                if (info != null && info.isConnected()) {
                    if (ConnectivityManager.TYPE_WIFI == info.getType()) {
                        Toast.makeText(context, "WIFI连上了,可以使用", Toast.LENGTH_SHORT).show();
                        isMobile = false;
                    } else if (ConnectivityManager.TYPE_MOBILE == info.getType()) {
                        Toast.makeText(context, "使用的是移动数据,请当心", Toast.LENGTH_SHORT).show();
                        isMobile = true;
                    }
                } else {
                    Toast.makeText(context, "没忘了", Toast.LENGTH_SHORT).show();
                }
                //设置网络状态
                ImageMode.ChangeMobile(isMobile);
            }
        }
    }
}
下面这里的Application只是为了得到一个Context
public class MyAppLication extends Application {

    public static MyAppLication myAppLication;

    @Override
    public void onCreate() {
        super.onCreate();
        myAppLication = this;
    }
    public static Context getMyApplication() {
        return myAppLication;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值