//创建一个Application
public class MyApplication extends Application {
private static MyApplication myApplication;
@Override
public void onCreate() {
super.onCreate();
myApplication = this;
}
public static Context getAppContext() {
return myApplication;
}
}
//记得在清单文件中配置
//记得加权限 访问网络权限和获取网络状态权限
//写一个工具类
public class NetUtils {
public static final String SP_NAME = "SP_NAME";
public static final String PICTURE_LOAD_MODE_KEY = "PICTURE_LOAD_MODE_KEY";
private boolean isMobileConnectivity = true;
private static final String BASE_URL_BIG_PICTURE = "http://www.big.picture";
private static final String BASE_URL_SMALL_PICTURE = "http://www.small.picture";
private static final String BASE_URL_NO_PICTURE = "http://www.no.picture";
//baseurl默认使用大图的模式
private String BASE_URL = BASE_URL_BIG_PICTURE;
private static NetUtils mNetUtils;
private NetUtils() {
}
//单例模式
public static NetUtils getInstance() {
if (mNetUtils == null) {
synchronized (NetUtils.class) {
if (mNetUtils == null) {
mNetUtils = new NetUtils();
}
}
}
return mNetUtils;
}
/**
* 使用get post请求的时候,调用这个方法获取我们的baseUrl,直接去请求就可以
*/
public String getBASE_URL() {
// TODO: 2017/8/10 根据网络状态和用户选择,选择对应的baseurl
if (isMobileConnectivity) {
TODO: 2017/8/10 根据用户选择 返回对应的url
int mode = MyApplication.getAppContext().getSharedPreferences(SP_NAME, Context.MODE_PRIVATE).getInt(PICTURE_LOAD_MODE_KEY, 0);
switch (mode) {
case 0:
BASE_URL = BASE_URL_BIG_PICTURE;
break;
case 1:
BASE_URL = BASE_URL_SMALL_PICTURE;
break;
case 2:
BASE_URL = BASE_URL_NO_PICTURE;
break;
}
} else {
BASE_URL = BASE_URL_BIG_PICTURE;
}
return BASE_URL;
}
//更改我们当前的网络状态
public void changeNetState(boolean isMobileConnectivity) {
this.isMobileConnectivity = isMobileConnectivity;
}
}
//MainActivity中
public class MainActivity extends AppCompatActivity {
ConnectivityBroadcastReceiver mConnectivityBroadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
mConnectivityBroadcastReceiver = new ConnectivityBroadcastReceiver();
registerReceiver(mConnectivityBroadcastReceiver, filter);
}
//测试一下
public void onTestBaseUrl(View v) {
Toast.makeText(MainActivity.this, NetUtils.getInstance().getBASE_URL(), Toast.LENGTH_SHORT).show();
}
public void onClick(View v) {
// TODO: 2017/8/10 做一个alertDialog
String[] strings = {"最佳效果", "较省流量", "极省流量"};
int mode = MyApplication.getAppContext().getSharedPreferences(NetUtils.SP_NAME, Context.MODE_PRIVATE).getInt(NetUtils.PICTURE_LOAD_MODE_KEY, 0);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("非wifi网络流量");
builder.setSingleChoiceItems(strings, mode, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
TODO: 2017/8/10 要保存我们的图片加载模式
MyApplication.getAppContext().getSharedPreferences(NetUtils.SP_NAME, Context.MODE_PRIVATE).edit().putInt(NetUtils.PICTURE_LOAD_MODE_KEY, which).commit();
dialog.dismiss();
}
});
builder.setNegativeButton("取消", null);
builder.create().show();
}
/**
* 接受网络状态的改变
*/
public class ConnectivityBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
boolean isMobileConnectivity = true;
//如果能走到这,说明网络已经发生变化
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
if (ConnectivityManager.TYPE_WIFI == activeNetworkInfo.getType()) {
Toast.makeText(MainActivity.this, "wifi可用,下载吧", Toast.LENGTH_SHORT).show();
isMobileConnectivity = false;
} else if (ConnectivityManager.TYPE_MOBILE == activeNetworkInfo.getType()) {
Toast.makeText(MainActivity.this, "现在是移动网络,当心", Toast.LENGTH_SHORT).show();
isMobileConnectivity = true;
//获得现在的网络状态 是移动网络,去改变我们的访问接口
} else {
Toast.makeText(MainActivity.this, "网络不可用,请检查网络", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "网络不可用,请检查网络", Toast.LENGTH_SHORT).show();
}
TODO: 2017/8/10 改变一下网络状态
NetUtils.getInstance().changeNetState(isMobileConnectivity);
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//解注册
unregisterReceiver(mConnectivityBroadcastReceiver);
}
}