想在网络请求之前进行一次“网络可用”的判断,如果网络不可用,我就不执行网络请求,直接return。
在android5.0是正常可以判断,并且弹出Toast。
但是android6.0的机子进行运行,不能弹出Toast。
网上搜索之后,有人说这是因为Toast不能用全局变量Context...还是迷茫不知道到底是怎么回事。望解答!
//检查网络方法,(context是从Activity得到传向HttpUtils的)
`private void checkNet(Context context){
isNetAvailable=NetWorkTools.isNetWork(context);
if(!isNetAvailable){
PromptManager.closeProgressDialog();
PromptManager.showToast(context, "网络不可用,请检查网络后再重试!");
return;
}
}
`
//HttpUtils的网络请求
public void login(Context context,HashMap<String, String> params, Response.Listener<String> successListener, Response.ErrorListener errorListener) {
checkNet(context);
System.out.println("params:" + params);
StringRequest request = new StringRequest(Method.POST, Constants.LOGIN, params, successListener,errorListener);
VolleyUtils.getInstance(mContext).addToRequestQueue(request);
}
望解答!谢谢!
PromptManager.showToast(); This method encapsulates the Toast class. You can directly use Toast to see if there is any problem with the 6.0 phone. If it is convenient, please also post the showToast(); method.
Toast, you can directly use the context of the application. In addition, if the mobile application turns off the notification option, you will not be able to see the Toast
What you said above is correct. You cannot see toast after turning off the notification permission in 6.0 and above. Moreover, there is no problem in using toast in my personal project (7.0). It is recommended to check the specific implementation logic.
1. Check whether this App notification is turned off on your phone. I have encountered this problem before.
2. Use the Toast.make().show() method directly where needed to check whether the Toast comes out
3. You can also use SnackBar to replace the Toast