6

I read in the documentations that the connect method "returns immediately, and connects to the service in the background." But I don't think it's the case, at least for me.

I even create a separate thread for the connect method but the UI still freezes every time onStart method (where I'm trying to connect) is called.

NOTE: This happens only when there is no internet connection.

So how do I get around this? Thanks

Sid Go
  • 1,824
  • 3
  • 13
  • 23
  • why are you calling connect when there is no internet. Before calling connect method first check if internet is available or not – Vivek Mishra Mar 16 '16 at 13:01
  • Yea. I'm checking it via the ConnectionManager. But by saying "no internet connection", I mean connection could be very slow. Or that the user is connected to a network/wifi but the wifi itself has no internet connection. – Sid Go Mar 16 '16 at 13:39
  • Or could you give me a better way to check the internet connection? Thanks. – Sid Go Mar 16 '16 at 13:40
  • Seee this link http://stackoverflow.com/questions/6493517/detect-if-android-device-has-internet-connection – Vivek Mishra Mar 16 '16 at 13:43
  • I had a similar issue. Do not create instance of GoogleApiClient if there is no internet. wrap instantiation in if(isInternet()){} and don't forget to annotate mGoogleApiClient with @Nullable – RafalManka Jul 11 '16 at 13:42
  • I am having a similar problem. By looking at the general logs, I can see an exception after 2 minutes (!) and the client still hanging there. – dariosalvi Aug 15 '16 at 15:45
  • Do you have an example of your code? – Nelson Wright Jun 13 '17 at 13:22
  • Can you provide logcat? – Margarita Litkevych Jun 14 '17 at 18:30

1 Answers1

1

Like said above check whether the you have internet connection..!!

 private void CheckConnection() {

        ConnectivityManager cm = (ConnectivityManager) getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (null != activeNetwork) {
            if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
                answer="You are connected to a WiFi Network";
            System.out.println(answer);
            if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                answer="You are connected to a Mobile Network";
        }
        else
            answer = "No internet Connectivity";
        Toast.makeText(getApplicationContext(), answer, Toast.LENGTH_LONG).show();
    }

In Manifest File add

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-feature android:name="android.hardware.location.gps" />

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

if in Activity

@Override
    protected void onStart() {
        super.onStart();
        if (!googleApiClient.isConnecting() || !googleApiClient.isConnected()) {
            googleApiClient.connect();
        }
    }

and In Oncreate function

 //Initializing googleApiClient
        googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        googleApiClient.connect();

in Service class do the above on Oncreate.