2

I have broadcast like this:

private final BroadcastReceiver locationReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().matches(LocationManager.PROVIDERS_CHANGED_ACTION)){
            //do stuff
            startLocationUpdates();
        }
    }
};

When the user turns on the gps, locationReceiver should executre the statLocationUpdates(). This is the code inside that method:

protected void startLocationUpdates() {
    try
    {
        LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
        {
            buildAlertMessageNoGps();
        }
    }
    catch (SecurityException ex)
    {

    }

There is a problem with line LocationServices.FusedLocationApi... I cant figure out why. But this is what the log says:

02-28 21:31:40.747 21273-21273/com.theapplabperu.hangover E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.theapplabperu.hangover, PID: 21273
                                                                        java.lang.RuntimeException: Error receiving broadcast Intent { act=android.location.PROVIDERS_CHANGED flg=0x10 } in com.theapplabperu.hangover.View.Activity.ActivitySplash$11@f6e8393
                                                                            at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:932)
                                                                            at android.os.Handler.handleCallback(Handler.java:815)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                            at android.os.Looper.loop(Looper.java:207)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5728)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
                                                                         Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet.

I read another post but I didn't get how to solve it yet. Any idea?

UPDATE:

I actually create my googleapiclient in the onCreate method:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    myContext = this;
    myActivity = this;
    mVisible = true;


    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(myContext)
                .addConnectionCallbacks(ActivitySplash.this)
                .addOnConnectionFailedListener(ActivitySplash.this)
                .addApi(LocationServices.API)
                .build();
    }
LordCommanDev
  • 922
  • 12
  • 38
  • Or don't register your broadcast receiver until you receive the GoogleApiClient 'onConnected' callback. –  Mar 01 '18 at 03:35

2 Answers2

2

It's on your exception:

Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet.

Please make sure that your mGoogleApiClient is connected before using it. You may use mGoogleApiClient.isConnected() to verify and mGoogleApiClient.connect() to connect it.

Edit:

Another thing you can do is to make sure you register the broadcast after the mGoogleApiClient has been connected. You may do so by placing it on the onConnected callback.

  • It is connected :/ – LordCommanDev Mar 01 '18 at 05:13
  • Based on your log, I don't think it is. One scenario might be you received the broadcast before the google client is connected. I've edited my answer for another suggestion – aldrinarciga Mar 01 '18 at 05:27
  • I found the problem. When I call for the first time LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); there is no problem. But, if I call again that line, the app crash. Do you know about it? – LordCommanDev Mar 01 '18 at 05:55
0

Try moving your googleApiClient creation to onCreate(). https://stackoverflow.com/a/29440779/4904995

Tung Tran
  • 2,885
  • 2
  • 17
  • 24