Display An Alert When Internet Connection Not Available in Android Application - Stack Overflow PDF
Display An Alert When Internet Connection Not Available in Android Application - Stack Overflow PDF
Questions
Tags
Users
sign up
Badges
Unanswered
log in
Ask
alert
In my application data comes from internet and I am trying to create a function that checks if a
internet connection is available or not and if it isn't, it gives an alert messege that no internet
connection available. i am using following code. but its not working.
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
if (isOnline())
{
// my code
}
else
{
Hotgames4meActivity1.this.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
try {
AlertDialog alertDialog = new AlertDialog.Builder(Hotgames4meActivity1.this).create();
alertDialog.setTitle("Info");
alertDialog.setMessage("Internet not available, Cross check your internet connectivity and try again");
//alertDialog.setIcon(R.drawable.alerticon);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
share
2 10
Asked
Apr 20 '12 at 7:57
Edited
Apr 20 '12 at 10:37
What problem you are facing with this method??? DeepSan Apr 20 '12 at 8:00
use toast to show message Zaz Gmy Apr 20 '12 at 8:07
@DeepSan:where i hav to put this code, i want to check connection before main activity create. thanks for
reply ZooZoo Apr 20 '12 at 8:15
you can call this method in onCreate method of your activity, whenever your activity will open i will work...
DeepSan Apr 20 '12 at 8:25
i call this in Oncreate. but it does not display any msg. n acitity dismiss. ZooZoo Apr 20 '12 at 8:34
order by votes
9 Answers
share
9 24
Sergey Metlov
8,615 18
55 106
Answered
Apr 20 '12 at 8:49
Edited
Aug 25 '14 at 12:41
hi DeepSan. i applied above code. but still, it is not showing any alret n app dismiss :(.. its sucks . plz help
ZooZoo Apr 20 '12 at 10:05
is there any exception??? or if possible post the code, because all the methods in this thread are working fine,
you please post your code... DeepSan Apr 20 '12 at 11:24
thanks for code.. its not working on emulator but working perfectly on device :) ZooZoo Apr 20 '12 at 11:29
welcome...good to hear.... DeepSan Apr 20 '12 at 11:32
add a comment
6
public boolean isOnline() {
ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemSer
vice(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
if(netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()){
Toast.makeText(context, "No Internet connection!", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
And you must add premission for accessing network state and Internet:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
share
Answered
Apr 20 '12 at 8:16
15
thanks, i made changes. can u plz suggest where to put this method. i want to check this before main activity
launched. ZooZoo Apr 20 '12 at 8:27
if (isOnline()==false) { YourActivity.this.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS)); }
vtuhtan Apr 20 '12 at 9:07
add a comment
share
8 17
Answered
Apr 20 '12 at 9:01
share
83
Answered
Apr 20 '12 at 8:01
add a comment
try this
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_S
ERVICE);
android.net.NetworkInfo wifi = cm
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo datac = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if ((wifi != null & datac != null)
&& (wifi.isConnected() | datac.isConnected())) {
//connection is avlilable
}else{
//no connection
Toast toast = Toast.makeText(context, "No Internet Connection",
Toast.LENGTH_LONG);
toast.show();
}
share
Answered
Apr 20 '12 at 8:04
i m wodering where to put this code. m wrting code in oncreate but its not working there. :( ZooZoo Apr 20
'12 at 8:12
Its better to put it in onResume() method.But that cannot be the problem. sampathpremarathna Apr 20 '12 at
8:16
but i want to cheak before activity created. if there is no internet, the main activity should not load n there is
alert msg . how to do .?? ZooZoo Apr 20 '12 at 8:25
It should work if u put it in onCreate() method also. sampathpremarathna Apr 20 '12 at 8:34
but its not.. i edited my code. plz see. help me where is bug. ZooZoo Apr 20 '12 at 10:40
The above method just informs you whether your mobile has the possibility to connect
to the internet, however, it does not tell exactly if connectivity exists.. for example, you
might be able to connect to a wifi, but be in a coffee shop where you should enter
credentials into a hot spot website... or , your home wifi might be working, and you are
connected to it, but cannot access internet. Use the below code to check for internet
connetivity. it is preferable to use this inside an asynctask.
public boolean hasActiveInternetConnection()
{
try
{
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").ope
nConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(4000);
urlc.setReadTimeout(4000);
urlc.connect();
networkcode2 = urlc.getResponseCode();
return (urlc.getResponseCode() == 200);
} catch (IOException e)
{
Log.i("warning", "Error checking internet connection", e);
return false;
}
}
share
12
Answered
Feb 15 '13 at 11:25
share
Sam
Answered
Sep 24 '14 at 15:27
share
-2
Answered
Feb 19 '14 at 5:49
Edited
Oct 18 '14 at 13:01
42
});
share
Answered
Mar 19 at 5:06
Floern
16.9k
Edited
Mar 19 at 9:08
10 47 70
This only displays an alert, but the question also requires checking if an internet connection is available or not
to know when to display the alert. Marilia yesterday
add a comment
Your Answer
log in
or
Name
By posting your answer, you agree to the privacy policy and terms of service.
meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc