选择图片加载模式

主界面:

public class MainActivity extends AppCompatActivity {
    Broad broad;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        broad = new Broad();
        registerReceiver(broad,filter);
    }
    public void TextView(View view){
        String[] string = {"最佳效果", "较省流量", "极省流量"};
        int mode = MyApplication.getAppContext().getSharedPreferences(Utils.SP_NAME, Context.MODE_PRIVATE).getInt(Utils.PICTURE_LOAD_MODE_KEY, 0);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("wifi网络流量");
        builder.setSingleChoiceItems(string, mode, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                MyApplication.getAppContext().getSharedPreferences(Utils.SP_NAME,Context.MODE_PRIVATE).edit().putInt(Utils.PICTURE_LOAD_MODE_KEY,which).commit();
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", null);
        builder.create().show()
    }
    public void Button(View view){
        Toast.makeText(MainActivity.this, Utils.getInstance().getBASE_URL(), Toast.LENGTH_SHORT).show();
    }
    public class Broad extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if(ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())){
                boolean wang = true;
                ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = manager.getActiveNetworkInfo();
                if(networkInfo != null && networkInfo.isConnected()){
                    if (ConnectivityManager.TYPE_WIFI == networkInfo.getType()) {
                        Toast.makeText(MainActivity.this, "wifi可用,下载吧", Toast.LENGTH_SHORT).show();
                        wang = false;
                    } else if (ConnectivityManager.TYPE_MOBILE == networkInfo.getType()) {
                      Toast.makeText(MainActivity.this, "现在是移动网络,当心", Toast.LENGTH_SHORT).show();
                        wang = true;
                        //获得现在的网络状态 是移动网络,去改变我们的访问接口
                    } else {
                   Toast.makeText(MainActivity.this, "网络不可用,请检查网络", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(MainActivity.this, "网络不可用,请检查网络", Toast.LENGTH_SHORT).show();
                }
                Utils.getInstance().changeNetState(wang);
            }
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解注册
        unregisterReceiver(broad);
    }
}
Utils类:

public class Utils {
    private static final String DATU = "http://www.big.picture";
    private static final String ZHONGTU = "http://www.small.picture";
    private static final String XIAOTU = "http://www.no.picture";
    private String BASE_URL=DATU;
    private static Utils utils;
    private boolean wang = true;
    public static final String SP_NAME = "SP_NAME";
    public static final String PICTURE_LOAD_MODE_KEY = "PICTURE_LOAD_MODE_KEY";

    private Utils(){

    }
    public static Utils getInstance() {
        if (utils == null) {
            synchronized (Utils.class) {
                if (utils == null) {
                    utils = new Utils();
                }
            }
        }
        return utils;
    }
    public String getBASE_URL(){
        if(wang){
            int mode = MyApplication.getAppContext().getSharedPreferences(SP_NAME, Context.MODE_PRIVATE).getInt(PICTURE_LOAD_MODE_KEY, 0);
            switch (mode){
                case 0:{
                    BASE_URL = DATU;
                }
                break;
                case 1:{
                    BASE_URL = ZHONGTU;
                }
                break;
                case 2:{
                    BASE_URL = XIAOTU;
                }
                break;
            }
        }else {
            BASE_URL = DATU;
        }
        return BASE_URL;
    }
    public void changeNetState(boolean wang) {
        this.wang = wang;
    }
}
MyApplication类:

public class MyApplication extends Application {
    private static MyApplication myApplication;
    @Override
    public void onCreate() {
        super.onCreate();
        myApplication = this;
    }
    public static Context getAppContext(){
        return myApplication;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值