主界面:
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; } }