Answer the question
In order to leave comments, you need to log in
Is it normal to check for a network by trying to download a file?
There is an asynctask that downloads a file from the Internet and, upon completion, a callback where I check the existence of the downloaded file - if downloaded, ok, go further if there is no error.
those. do not check the availability of the Internet through the wifi or network, because the real check is only by ping, and this is a long time for the application, and immediately download - the file is not a large text file.
Thank you.
Answer the question
In order to leave comments, you need to log in
This is a possible solution, but not normal.
All network operations of this type cannot be carried out in an AsyncTask, it is not viable outside the context of an Activity. If the user closes the application and the system kills the Activity, which is very likely, the download process is over.
It will be correct to use the service + catching network errors.
This is a normal IntentService . And catching errors is done using the Exception class .
It will be useful to read about how developer.android.com recommends handling such errors: here .
private boolean isConnected() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni != null && ni.isConnected()) {
return true;
} else {
return false;
}
}
From working draft)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question