0

i want to ask a question which may is very simple for some of you.

How can i figure out, whether my Application has an internet connection?

I want to connect to an MySQL Database and when there is no Internet, there should be an AlertDialog.

    new AsyncTask() {
        ProgressDialog dialog = ProgressDialog.show(AppActivity.this, "Lade", "Daten werden abgerufen...", true);
        @Override
        protected void onPostExecute(Object result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            dialog.dismiss();
            max = Name.size()-1;
            kontrolle = new boolean [max*2];

            pb.setMax(max/2);

            java.util.Arrays.fill(kontrolle, false);

            bilder();
        }

        @Override
        protected Object doInBackground(Object... arg0) 
        {
            // TODO Auto-generated method stub
               dialog.show();
               getData();
            return null;
        }
    }.execute();

 private void getData() 
{
    String result = "";
    ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();

    try
    {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://.../read.php");
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        is=entity.getContent();
    }
    catch(Exception e)
    {
        Log.e("log-tag","Keine Verbindung"+e.toString());
        Toast.makeText(getApplicationContext(), "Keine Verbindung!", Toast.LENGTH_SHORT).show();
    }

    try
    {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = "";

        while((line= reader.readLine())!=null)
                {
                    sb.append(line+"n");
                }
        is.close();
        result = sb.toString();
         result.trim();
    }
    catch(Exception e)
    {
        Log.e("log-tag","Error"+e.toString());
    }
    try
    {
        JSONArray jArray = new JSONArray(result);
        for(int i=0;i<jArray.length();i++)
        {
            JSONObject json_data = jArray.getJSONObject(i);


            ID.add((String) json_data.get("id"));
            Name.add((String) json_data.get("name"));
            Pts.add((String)json_data.get("pts"));
        }
    }
    catch(Exception e)
    {
        Log.e("log-tag","Error2"+e.toString());
    }
}

How can i achieve that? i have no idea i thought this

catch(Exception e)
{
    Log.e("log-tag","Keine Verbindung"+e.toString());
    Toast.makeText(getApplicationContext(), "Keine Verbindung!", Toast.LENGTH_SHORT).show();
}

would do the work, but when i have no connection (because i set my phone in flight mode) there is just an endless ProgressDialog :(

1 Answers1

1

As Jon Taylor mentioned this is a duplicate question

you can use the following code to do this

this method checks whether mobile is connected to internet and returns true if connected and displays an alert box if not:

private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
// There are no active networks.
AlertDialog.Builder altDialog= new AlertDialog.Builder(this);
altDialog.setMessage("No network connection!");
return false;
} else
return true;
}

add this to the manifest file,

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

EDIT: To get the network type you can use this code snippet:

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

//mobile
State mobile = conMan.getNetworkInfo(0).getState();

//wifi
State wifi = conMan.getNetworkInfo(1).getState();

and then use it like that:

if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
//mobile
} else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
//wifi
}
Community
  • 1
  • 1
Hip Hip Array
  • 4,665
  • 11
  • 49
  • 80