Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 10th, 2012  |  syntax: None  |  size: 0.94 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Android app crashing with no internet [closed]
  2. btn1.setOnClickListener(new OnClickListener()
  3.     {
  4.         public void onClick(View v)
  5.         {
  6.             SetPage();
  7.         }
  8.     });
  9.  
  10.     private void SetPage()
  11. {
  12.     if (isOnline() == true)
  13.         startActivity(new Intent(getApplicationContext(), PageActivity.class));
  14.     else{
  15.         NoInternet();
  16.     }
  17. }
  18.  
  19.     private void NoInternet()
  20. {
  21.     Toast toast;
  22.     Context context = getApplicationContext();
  23.     toast = Toast.makeText(context, "No internet connection", Toast.LENGTH_SHORT);
  24.     toast.show();
  25. }
  26.  
  27. private boolean isOnline()
  28. {
  29.     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  30.     NetworkInfo[] ni = cm.getAllNetworkInfo();
  31.     if (ni != null){
  32.         for (int i = 0; i < ni.length; i++){
  33.             if (ni[i].getState() == NetworkInfo.State.CONNECTED){
  34.                 return true;
  35.             }
  36.         }
  37.     }
  38.         return false;
  39. }