-1

My app main method look like this:

public class MainActivity extends Activity{

    ImageButton btnRegister, btnConfig, btnSurvey, btnUpload;
    SQLiteDatabase profile;
    TextView txtv;
    TextView tv;
    EditText et;
    TextView txtLat;
    TextView txtLong;
    Spinner spinner;
    List<Integer> editTextIdList = new ArrayList<Integer>();
     int id = 0;
     Button btnLatLng;
     List<String> assetArray = new ArrayList<String>();
     //Upload server 

        private Socket socket;
        private static final int SERVERPORT = 6000;
        private static final String SERVER_IP = "115.254.100.2";


     //GPSTracker class
     GPSTracker gps;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        isNetworkConnected();
        initializer();

         //Thread initialize
         new Thread(new ClientThread()).start();
}

public boolean isNetworkConnected() {
        final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
        return activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED;
   }

It requires internet access. But without internet access it got crashed. How can I avoid crashing of application, Instaed can I give an Toast meassge ane exit the app without crash?? How can I do that?

androidGenX
  • 1,108
  • 3
  • 18
  • 44

4 Answers4

3

Try this :

ConnectivityManager conMgr =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED 
||  conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING  ) {
//notify  you are online
Toast.makeText(getApplicationContext(), "No Internet access available", 
Toast.LENGTH_LONG).show();
}
else if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED 
||  conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
//notify  you are not online
 Toast.makeText(getApplicationContext(), "No Internet access available", 
Toast.LENGTH_LONG).show();
}

You must include :

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

in your Manifest.xml file

Arshad Ali
  • 3,082
  • 12
  • 56
  • 99
1

How about this?:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 
    if (isNetworkConnected())
{
  initializer();
  //Thread initialize
   new Thread(new ClientThread()).start();
}
else
{
//not connected
}

}
anotherBug
  • 117
  • 4
0

have you given following permission in manifest.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
user8938
  • 559
  • 1
  • 4
  • 12
0
public boolean isConnectingToInternet()
    {
        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) 
        {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }

        }
        return false;
    }
user8938
  • 559
  • 1
  • 4
  • 12