-2

I develop an application which is fully internet base.But i am stack in a point. the point is When user did not connect to the internet the app is stop.

I use bellow code.

public class MainActivity extends ActionBarActivity {
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
private AdView mAdView;
private InterstitialAd interstitial;
private long lastPressedTime;
static String imgURL = "url";
static String VIDEO_ID = "videoId";
static String TITLE = "title";
static String THUMBNAILS = "img";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from listview_main.xml
    setContentView(R.layout.listview_main);
    // Execute DownloadJSON AsyncTask
    new DownloadJSON().execute();


    isNetworkAvailable();

}

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

how can i solve this??

Amit Saha
  • 121
  • 1
  • 10
  • i think this answer may be helpful to you http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts – ZAIRI Oussama Feb 04 '16 at 17:55
  • common man. Search for "how to check if internet is available in android" and you will get tons of tutorials. – thedarkpassenger Feb 04 '16 at 18:09
  • BTW it depends with what you wanna look for, Im sure you mean, you want to detect if wifi or 3G/4G is successfully connected and has internet service. if that is the case look at this posting http://stackoverflow.com/questions/9570237/android-check-internet-connection – CodeDaily Feb 04 '16 at 18:13

2 Answers2

0

You should do it like this

boolean isInternet=isNetworkAvailable();

if(isInternet)
{
new DownloadJSON().execute();
}
else
{
//show toast message
}
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
0
// Checks Internet Status
public boolean isOnline() {
    if(cm == null)
        return false;

    final NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED) 
        return true;

    return false;
}

and in Manifest

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Sav4yk
  • 25
  • 5