0

that showing google map API v2 with android. sometimes my app crash when no internet connection, like when i put GOOGLE Place API. when i clicked ATM and no internet then app will crash. i want to make a exception , so when no internet connection, then the map can show a notification that phone doest have a internet connection.

found soulution for this problem http://www.androidhive.info/2012/07/android-detect-internet-connection-status/

kucluk
  • 540
  • 2
  • 9
  • 29
  • why don't you put some code that checks for internet activity once the app starts? [chech this link](http://stackoverflow.com/questions/4238921/android-detect-whether-there-is-an-internet-connection-available) – Taslim A. Khan Sep 11 '13 at 17:49
  • alr used that code, but sometimes the internet not available, so apps will crash – kucluk Sep 18 '13 at 11:19
  • did you check it with a device? what sort of internet connection are you using? on a device, if you are using a wifi connection, codes that were put earlier as answers will work, but if you are using data connection(mobile operator internet) then these codes might not work. Secondly, I have a feeling, but not sure, the codes available here does not work when you check for internet conectivity on an emulator. Please specify what type of connection you are checking. – Taslim A. Khan Sep 18 '13 at 12:57

3 Answers3

1

Try to Put this code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (servicesOK()) {
        Toast.makeText(this, "Ready to map!", Toast.LENGTH_LONG).show();
        setContentView(R.layout.testmap);

    } else {
        setContentView(R.layout.activitymain);

    }


}

public boolean servicesOK() {

    int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if (isAvailable == ConnectionResult.SUCCESS) {

        return true;

    } else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {

        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
                this, GPS_ERRORDIALOG_REQUEST);
        dialog.show();

    } else {

        Toast.makeText(this, "Can not connect!", Toast.LENGTH_SHORT).show();

    }
    return false;
}

This might help you..:)

Dinithe Pieris
  • 1,822
  • 3
  • 32
  • 44
mike20132013
  • 5,357
  • 3
  • 31
  • 41
  • Hi Mike i still cannot resolved that problem, would you mind to check the code here http://wptrafficanalyzer.in/blog/showing-nearby-places-using-google-places-api-and-google-map-android-api-v2/ and how when no internet connections and users click FIND button. – kucluk Sep 18 '13 at 11:20
  • Hi mike this is mainactivity code and JSON parser http://pastebin.com/hkQSwEHE this JSON Parser http://pastebin.com/v7MabBdc – kucluk Sep 18 '13 at 15:31
  • Hey..Thanks for the info.. Did you check with the Manifest ? are all the permissions in place ? Most importantly, the internet permission ? – mike20132013 Sep 18 '13 at 17:12
  • Hi mike its not about manifest. its about how to make a exception or Try catch, so when no internet connection the apps will not crash – kucluk Sep 19 '13 at 06:08
0

You can use this method to check if there is an internet connection on the device:

public boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager
            .getActiveNetworkInfo();
    return activeNetworkInfo != null;
}
Emmanuel
  • 13,083
  • 4
  • 39
  • 53
  • not like this. i have JSON parsing which is parse Google Places Nearby. so when i click nearby place without internet connection the map will crash – kucluk Sep 15 '13 at 19:32
0

use an if statement to handle the crash.

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

if (isNetworkAvailable == true){
//your map code
} else {
Log.e("Error","No network");
 Toast.makeText(this, "no internet" , Toast.LENGTH_LONG).show();
 }
}

don't forget to have checking the network permission in your manifest.

*the method is taken from Emmanuel's answer

Ahmed Ekri
  • 4,601
  • 3
  • 23
  • 42