Need to do: When the user clicks on the download button, the file download starts. If there is no internet: this message is displayed. How to implement?
Closed due to the fact that the essence of the issue is not clear to the participants of Kromster , Vartlok , cheops , user194374, lexxl 10 Aug '16 at 7:23 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
|
2 answers
create an AppController file
import android.app.Application; import android.text.TextUtils; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.toolbox.Volley; public class AppController extends Application { public static final String TAG = AppController.class.getSimpleName(); private RequestQueue mRequestQueue; private static AppController mInstance; @Override public void onCreate() { super.onCreate(); mInstance = this; } public static synchronized AppController getInstance() { return mInstance; } public RequestQueue getRequestQueue() { if (mRequestQueue == null) { mRequestQueue = Volley.newRequestQueue(getApplicationContext()); } return mRequestQueue; } public <T> void addToRequestQueue(Request<T> req, String tag) { req.setTag(TextUtils.isEmpty(tag) ? TAG : tag); getRequestQueue().add(req); } } Add to manifest
<application android:name=".AppController" ... /> Add to build.gradle
dependencies { ... compile 'com.mcxiaoke.volley:library-aar:1.0.0' } And finally, the method call itself, it works asynchronously, so you do not need to give it AsyncTask or something else, below is an example for the line, you can also create a method for another file by reference .
String tag_string_req = "request"; final ProgressDialog progressDialog = new ProgressDialog(getContext()); progressDialog.setMessage("Подключение к серверу. Подождите..."); progressDialog.setIndeterminate(false); progressDialog.setCancelable(false); progressDialog.show(); StringRequest strReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { progressDialog.dismiss(); Log.d("response",response); }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { progressDialog.dismiss(); AlertDialog.Builder alert = new AlertDialog.Builder(getContext()); alert.setTitle("Ошибка авторизации"); final TextView input = new TextView(getContext()); alert.setView(input); input.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17); input.setPadding(20, 10, 20, 10); input.setTextColor(ContextCompat.getColor(getContext(), android.R.color.black)); input.setText("Ошибка"); alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }); alert.show(); } }) { @Override protected Map<String, String> getParams() { //Записываем параметры в запрос который передаем на сервер Map<String, String> params = new HashMap<>(); params.put("param", param); return params; } }; AppController.getInstance().addToRequestQueue(strReq, tag_string_req); |
On the button, do a check:
public static boolean hasConnection(final Context context) { ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifiInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (wifiInfo != null && wifiInfo.isConnected()) { return true; } wifiInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (wifiInfo != null && wifiInfo.isConnected()) { return true; } wifiInfo = cm.getActiveNetworkInfo(); if (wifiInfo != null && wifiInfo.isConnected()) { return true; } return false; } Then, if true is returned, download.
|