我正在检查互联网连接性,请调用一个方法(如果可用),否则显示一个警告对话框。
我正在调用的方法使用凌空来从Internet上获取数据,因此在调用onResponse
之前我给了它一个循环的ProgressDialog。
在onResponsese
中,我关闭了“进度”对话框。
现在的问题是,从调用getData
到调用onResponse
到互联网连接之间
当然,ProgressDialog不会停止加载。换句话说,即使没有希望提取数据的时间,它也会一直显示。
因此,我想在互联网连接断开时再次显示alertDialogBuilder
,并在仍然显示时关闭mProgressDialog
。
我以为可以用广播接收器完成此操作,因此我创建了一个接收器并将其注册到活动中,但是这是我所能做到的。
这是我的代码:
网络检查
public class NetworkCheck {
//I use this class to check for internet connectivity.
public static boolean isAvailableAndConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isNetworkAvailable = cm.getActiveNetworkInfo() != null;
boolean isNetWorkConnected = isNetworkAvailable && cm.getActiveNetworkInfo().isConnected();
return isNetWorkConnected;
}
}
互联网接收者
public class InternetReciever extends BroadcastReceiver {
public InternetReciever() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
throw new UnsupportedOperationException("Not yet implemented");
}
}
主要活动
public class MainActivity extends AppCompatActivity {
private final String TAG = "MainActivity";
//Creating a list of fruits
private List<FruitItems> mFruitItemsList;
//Creating Views
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private ProgressDialog mProgressDialog;
private int number = 2;
public int getNumber() {
return number++;
}
public void setNumber(int number) {
this.number = number;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//The usual codes in onCreate are I here
if (savedInstanceState != null && savedInstanceState.containsKey(KEY_FRUIT_ITEMS)) {
mFruitItemsList = savedInstanceState.getParcelableArrayList(KEY_FRUIT_ITEMS);
} else {
//Initializing the fruitlist
mFruitItemsList = new ArrayList<>();
if (NetworkCheck.isAvailableAndConnected(this)) {
getData();
} else {
final Context mContext;
mContext = this;
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle(R.string.alert_titl);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setIcon(R.mipmap.ic_launcher);
alertDialogBuilder.setMessage(R.string.alert_mess);
alertDialogBuilder.setPositiveButton(R.string.alert_retry, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (!NetworkCheck.isAvailableAndConnected(mContext)) {
alertDialogBuilder.show();
} else {
getData();
}
}
});
alertDialogBuilder.setNegativeButton(R.string.alert_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alertDialogBuilder.show();
}
}
//Registering Receivers
IntentFilter intentFilter = new IntentFilter();
InternetReciever internetReciever = new InternetReciever();
registerReceiver(internetReciever, intentFilter);
}
//This method will get data from the web api
private void getData(){
Log.d(TAG, "getData called");
//Showing progress dialog
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setCancelable(false);
mProgressDialog.setMessage(this.getResources().getString(R.string.load_fruit));
mProgressDialog.show();
//Creating a json request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(ConfigFruit.GET_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, "onResponse called");
//Dismissing the progress dialog
if (mProgressDialog != null) {
mProgressDialog.hide();
}
//calling method to parse json array
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(jsonArrayRequest);
}
//The method that parses json data is here
}
而且我也在考虑:在调用
mProgressDialog
之前关闭onResponse
时,是否不能取消截击请求? 最佳答案
因为在getData调用到onResponse调用凌空方法之间,您将无法上网
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (mProgressDialog != null) {
mProgressDialog.hide();
}
}
});
将被称为。您可以在此处显示警报。显示对数据的调用失败,并且也关闭了进度栏。
Response.ErrorListener
被调用后的原因是:其Volley提供的默认错误处理。所以我们最好用
提供的默认功能:)
关于android - 我应该如何编码此广播接收器以检查Internet并根据其执行操作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36661180/