You are on page 1of 8

Stack Overflow

Questions

Tags

Users

sign up
Badges

Unanswered

log in

Ask

Read this post in our app!

Display an alert when internet connection not available in android


application
android

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

improve this question


ZooZoo
48 1

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

show 2 more comments

order by votes

9 Answers

public void onCreate(Bundle obj)


{
super.onCreate(obj)
setContextView(layout);
if (isOnline())
//do whatever you want to do
else
{
try {
AlertDialog alertDialog = new AlertDialog.Builder(con).create();
alertDialog.setTitle("Info");
alertDialog.setMessage("Internet not available, Cross check your internet connectivity and try
again");
alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}

share

improve this answer


DeepSan
1,399 2

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

improve this answer


vtuhtan
698 4

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

Maybe try this


handler.removeCallbacks(checkInternetConnection);
handler.postDelayed(checkInternetConnection, UPDATE_INTERVAL);

public Runnable checkInternetConnection = new Runnable() {


public void run() {
handler.postDelayed(checkInternetConnection, UPDATE_INTERVAL);
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CON
NECTIVITY_SERVICE);
if(conMgr.getActiveNetworkInfo()!=null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected()){
alertOff();
}
else{
alertOn();

share

improve this answer


Boe-Dev
978 1

8 17

Answered
Apr 20 '12 at 9:01

add permission in your manifest.xml


<uses-permission android:name="android.permission.INTERNET" />

share

improve this answer


Padma Kumar
14k 12 49

83

Answered
Apr 20 '12 at 8:01

i already added.. ZooZoo Apr 20 '12 at 8:12

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();
}

and don't forget to add following permssions


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

share

improve this answer


sampathpremarathna
2,799 2 13 27

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

show 1 more comment

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

improve this answer


Dan Jurgen
696 7

12

Answered
Feb 15 '13 at 11:25

private boolean isNetworkAvailable() {


ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
if(!isNetworkAvailable()){
//Toast.makeText(this, "No Internet Connection", Toast.LENGTH_SHORT).show();
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Closing the App")
.setMessage("No Internet Connection,check your settings")
.setPositiveButton("Close", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})

share

improve this answer


Community Wiki

Sam
Answered
Sep 24 '14 at 15:27

public boolean isOnline()


{
ConnectivityManager connectionManager;
if(app_context!=null)
connectionManager = (ConnectivityManager) app_context.getSystemService(Context.CONNECTI
VITY_SERVICE);
else
return false;
try
{
if (connectionManager.getActiveNetworkInfo().isConnected())
{
Log.e(THIS_FILE, "Communicator ....isConnected()");
return true;
}

set permission in manifest


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

share

improve this answer


user2914699
77 1 6
IntelliJ Amiya
13.2k 2 21

-2

Answered
Feb 19 '14 at 5:49

Edited
Oct 18 '14 at 13:01

42

This is working in my code, try this:


public static void showNetDisabledAlertToUser(final Context context)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context, AlertDialog.THEME_T
RADITIONAL);
alertDialogBuilder
.setMessage("No internet connection on your device. Would you like to enable it?")
.setTitle("No Internet Connection")
.setCancelable(false)
.setPositiveButton(" Enable Internet ",
new DialogInterface.OnClickListener()
{

});

share

public void onClick(DialogInterface dialog, int id)


{
Intent dialogIntent = new Intent(android.provider.Settings.ACTION_SETTINGS);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(dialogIntent);
}

improve this answer


Mahesh Gawhane
1 1

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

Email

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

Post Your Answer

You might also like