Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. package com.example.alex.helppeopletogether.SupportClasses;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Context;
  5. import android.net.ConnectivityManager;
  6. import android.net.NetworkInfo;
  7. import android.os.AsyncTask;
  8. import android.support.design.widget.Snackbar;
  9. import android.view.View;
  10. import android.widget.TextView;
  11.  
  12. import com.example.alex.helppeopletogether.R;
  13.  
  14. import java.io.IOException;
  15. import java.net.HttpURLConnection;
  16. import java.net.MalformedURLException;
  17. import java.net.URL;
  18.  
  19. /**
  20. * Created by PM on 22.06.2016.
  21. */
  22. public class InternetCheck extends AsyncTask<String, String, Boolean> {
  23. Context context;
  24. View view;
  25. ProgressDialog nDialog;
  26.  
  27.  
  28. public InternetCheck(Context context, View view) {
  29. this.context = context;
  30. this.view = view;
  31.  
  32. }
  33.  
  34. @Override
  35. protected void onPreExecute() {
  36. super.onPreExecute();
  37. nDialog = new ProgressDialog(context);
  38. nDialog.setTitle(context.getString(R.string.check_internet_connection));
  39. nDialog.setMessage(context.getString(R.string.please_wait));
  40. nDialog.setIndeterminate(true);
  41. nDialog.setCancelable(false);
  42. nDialog.show();
  43. }
  44.  
  45. @Override
  46. protected Boolean doInBackground(String... args) {
  47. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  48. NetworkInfo netInfo = cm.getActiveNetworkInfo();
  49. if (netInfo != null && netInfo.isConnected()) {
  50. try {
  51. URL url = new URL("https://www.google.ru/");
  52. HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
  53. urlc.setConnectTimeout(3000);
  54. urlc.connect();
  55. if (urlc.getResponseCode() == 200) {
  56. return true;
  57. }
  58. } catch (MalformedURLException e1) {
  59. e1.printStackTrace();
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. return false;
  65. }
  66.  
  67. @Override
  68. protected void onPostExecute(Boolean th) {
  69. if (th == true) {
  70. nDialog.dismiss();
  71. //вызов активити или метода
  72. } else {
  73. nDialog.dismiss();
  74. snackBar();
  75. //loginErrorMsg.setText("Связь с интернетом отсутствует");
  76. }
  77. }
  78.  
  79. public void snackBar() {
  80. try {
  81. Snackbar snackbar = Snackbar.make(view, R.string.no_internet, Snackbar.LENGTH_SHORT);
  82. // Changing message text color
  83. // Changing action button text color
  84. View sbView = snackbar.getView();
  85. TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
  86. textView.setTextColor(context.getResources().getColor(R.color.blue));
  87. textView.setTextSize(27);
  88. snackbar.show();
  89. } catch (NullPointerException e) {
  90. return;
  91. }
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement