Last active
March 2, 2019 18:31
-
-
Save dynoChris/c611f22b0da700d048e08f141e7e5f45 to your computer and use it in GitHub Desktop.
How to make a check on the Internet Connection in Android. Call in AsyncTask with WeakReference for avoid memory leaks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CheckInternetConnectAsyncTask extends AsyncTask<Void, Void, Boolean> { | |
private final WeakReference<Activity> mActivityWeakReference; | |
public CheckInternetConnectAsyncTask(Activity activity) { | |
mActivityWeakReference = new WeakReference<>(activity); | |
} | |
@Override | |
protected void onPreExecute() { | |
super.onPreExecute(); | |
Activity activity = mActivityWeakReference.get(); | |
if (activity != null) { | |
ProgressBar progressBar = activity.findViewById(R.id.progress_bar); | |
progressBar.visibility(View.VISIBLE); | |
} | |
} | |
@Override | |
protected Boolean doInBackground(Void... voids) { | |
return NetworkUtils.isInternetConnected(); | |
} | |
@Override | |
protected void onPostExecute(Boolean isConnected) { | |
super.onPostExecute(isConnected); | |
Activity activity = mActivityWeakReference.get(); | |
if (activity != null) { | |
if (isConnected) { | |
//handle | |
} else { | |
ProgressBar progressBar = activity.findViewById(R.id.progress_bar); | |
progressBar.visibility(View.GONE); | |
CoordinatorLayout parentLayout = activity.findViewById(R.id.coordinator_layout); | |
Utils.showSnackbar(parentLayout, activity.getString(R.string.no_internet)); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
private CheckInternetConnectAsyncTask mCheckInternetConnect; | |
private Button mCheckInternetConnectButton; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mCheckInternetConnectButton = findViewById(R.id.check_internet_btn); | |
mCheckInternetConnectButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
mCheckInternetConnect = new CheckInternetConnectAsyncTask(MainActivity.this); | |
mCheckInternetConnect.execute(); | |
} | |
}); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
if (mCheckInternetConnect != null) mCheckInternetConnect.cancel(true); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NetworkUtils { | |
public static boolean isInternetConnected() { | |
try { | |
Socket sock = new Socket(); | |
sock.connect(new InetSocketAddress("8.8.8.8", 53), 1500); | |
sock.close(); | |
return true; | |
} catch (IOException e) { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment