This question already has an answer here:
I want to create an app that uses the internet and I'm trying to create a function that checks if a connection is available and if it isn't, go to an activity that has a retry button and an explanation.
Attached is my code so far, but I'm getting the error Syntax error, insert "}" to complete MethodBody.
Now I have been placing these in trying to get it to work, but so far no luck... Any help would be appreciated.
public class TheEvoStikLeagueActivity extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 3000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
private boolean checkInternetConnection() {
ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected()) {
return true;
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable() {
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(TheEvoStikLeagueActivity.this, IntroActivity.class);
TheEvoStikLeagueActivity.this.startActivity(mainIntent);
TheEvoStikLeagueActivity.this.finish();
}
}, SPLASH_DISPLAY_LENGHT);
} else {
return false;
Intent connectionIntent = new Intent(TheEvoStikLeagueActivity.this, HomeActivity.class);
TheEvoStikLeagueActivity.this.startActivity(connectionIntent);
TheEvoStikLeagueActivity.this.finish();
}
}
}
Try the following code:
in manifest
in code,
The above methods work when you are connected to a Wi-Fi source or via cell phone data packs. But in case of Wi-Fi connection there are cases when you are further asked to Sign-In like in Cafe. So in that case your application will fail as you are connected to Wi-Fi source but not with the Internet.
This method works fine.
Please use this in a separate thread from the main thread as it makes a network call and will throw NetwrokOnMainThreadException if not followed.
And also do not put this method inside onCreate or any other method. Put it inside a class and access it.
Check to make sure it is "connected" to a network:
Check to make sure it is "connected" to a network:
Permission needed:
https://stackoverflow.com/a/17583324/950427
I had issues with the IsInternetAvailable answer not testing for cellular networks, rather only if wifi was connected. This answer works for both wifi and mobile data:
How to check network connection enable or disable in WIFI and 3G(data plan) in mobile?
You can use following snippet to check Internet Connection.
You just have to copy following class and paste directly in your package.
Now you can use like:
DON'T FORGET to TAKE Permission :) :)
You can modify based on your requirement.
Thank you.