The method for connecting Google API clients is to freeze the user interface when there is no Internet connection

In the docs, I read that the connect method "returns immediately and connects to the service in the background." But I do not think that this is the case, at least for me.

I even create a separate thread for the connect method, but the user interface still hangs every time the onStart method is called (where I try to connect).

NOTE. This only happens if you are not connected to the Internet.

So how do I get around this? thanks

+6
source share
1 answer

As stated above, check if you have an 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 the 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 action

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

and in function oncreate

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

in the Service class, do the following in Oncreate .

+1
source

All Articles