I am new to java and android but have set myself a challenge to create an app that my students can use to interact with some of the content in my class (which I hope to have ready for them by the next semester).
I would like the students to be able to sign in to the app with their email address (Google account) and the app also includes a quiz for each section of work (for which their performance is emailed back to me). I would like to ensure that at active connection is available before a network related task is executed. I have looked at a number of SO questions (Detect if android device is connected to the internet ; Detect whether there is an Internet connection available on Android ; How to check internet access on Android? InetAddress never times out ; Checking internet connection on android , How to check internet access on Android? InetAddress never times out ; How to check internet access on Android? InetAddress never times out ; ...) in trying to formulate my own code, but I am not really understanding how to use the AsyncTask to ensure the network request is not being executed by the main thread to prevent getting the NetworkOnMainThreadException. Please could someone guide me to the correct way to use the AsycTask or Threads to perform the Internet Check before performing the sign-in or grades-sending actions in my app.
At the moment I am calling my isNetworkAvailable method from my Activity that handles the sign in activity as follows:
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isNetworkAvailable(getApplicationContext())) {
progressDialog.show();
Intent intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(intent, LOGIN_GOOGLE_NUMBER);
} else {
Toast.makeText(getApplicationContext(), "Please ensure that you are connected to the Internet and then try again", Toast.LENGTH_LONG).show();
}
}
});
The method in for checking the Internet connectivity which is part of the Main Signin class looks like this:
// Check if the network is connected (and if Internet is working)
private boolean isNetworkAvailable(Context context) {
//final ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
//return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected();
boolean connected = false;
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
//we are connected to a network
connected = true;
} else
connected = false;
if (connected) {
checkConnectivity isConnected = new checkConnectivity();
if (isConnected.hasInternetAccess(getApplicationContext())) {
return true;
} else
return false;
} else
return false;
}
and makes reference to the AsyncTask that I wanted to be accessible throughout the app as a separate public class:
public class checkConnectivity extends AsyncTask<Activity, Void, Boolean> {
private static final String TAG = "MyApplication Context";
protected Boolean doInBackground(Activity... activitys) {
return hasInternetAccess(activitys[0]);
}
// To check for internet before performing sign in activity
public boolean hasInternetAccess(Context context) {
try {
HttpURLConnection urlc = (HttpURLConnection)
(new URL("http://clients3.google.com/generate_204")
.openConnection());
urlc.setRequestProperty("User-Agent", "Android");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 204 &&
urlc.getContentLength() == 0);
} catch (IOException e) {
Log.e(TAG, "Error checking internet connection", e);
}
return false;
}
}
I have been coding this incrementally. If I just have the section to test the network connection then it works fine, but when I try and add the http url request then I run into problems.
What is the correct way to define a public class that I can use in different places in the app to ensure that there is an Internet connection, and how do I make the call in that specific activity? Any help will be most appreciated.