Fused Location sometimes STOPS

别说谁变了你拦得住时间么 提交于 2019-12-22 08:37:21

问题


I've been working on an app that tracks the user's position. For this I have used the google play services location module (a.k.a. fused location). All in all everything works fine.

BUT sometimes, completely random, I no longer receive location updates from the google location services, at all! I mean, my app is working fine, but no location updates. Not only that,if I start up google maps it can't get a fix on my location (even with GPS turned on). Reinstalling my app doesn't fix the issue. Out of the 3 taxi apps that I have on my phone, 2 of them can pinpoint my location. (Most likely they are using the old LocationManager to get the device's location).

I found that to fix this issue I have but to restart my device. But this is very unsettling, I can't expect my users to restart their devices whenever something goes wrong with the location updates. (though this has only happened to me about 6 times in the past 2 months)

So: Does anyone know about this issue and is there a work-around for it? Is my app somehow crashing google play services?


回答1:


I have recently experienced the same problem, Sebek. I do not believe the answer NYX gave is the correct one. There seems to be some other fix for the problem that only Google knows. Why do I say this?

I have created an app that uses Fused Location Services to get the location of the mobile device at 30 second intervals and toast the position when the app is in focus. But for some reason the Fused Services just stopped working. I tried again and again and again...switched on all location services - WIFI, GPS and Network...nothing worked. The Fused Location services remained off.

But then I tried something...I started up my app and then using the inbuilt Android Task Manager I launched Google Maps...and BOOM! The Fused Location Services on my app worked and started reporting location data on the 30-second mark everytime...and for some weird reason the accuracy of the locations was also improved...up to 5 metre accuracy!! I tried the same procedure over and over - first launch my app and nothing...then launch Maps and BOOM! - it worked everytime. A 100% success when I launched Google Maps.

So this is clear proof that Google is running some code that ensures the Fused Location Service doesn't drop out when Google apps are launched and needs the location service. Sebek, maybe you can try this and see if you get the same results. Maybe then we can progress somewhere in finding a true fix.

Cheers, Shore-T ...




回答2:


Try change location mode to "GPS only" and than turn back to "Hight Accuracy". The problem occurs sometimes when you are indoor and this fix it.

See here how to switch GPS programatically on/off




回答3:


I have found that if you don't unsubscribe from the LocationManager by calling removeUpdates on app close, it will sometimes cause the issue you are experiencing.




回答4:


I have been struggling with this problem for several weeks , I believe the android OS restrict an app which is in the background this means the app cant access to gps after one or two hours when it is in the background . may be because of less battery usage .

the solution I am using is this :

this is BaseActivity which is used for every activity :

 public abstract class BaseActivity extends AppCompatActivity {
 @Override
 protected void onPause() {
     super.onPause();
       MyApplication.activityPaused();
 }
 @Override
 protected void onResume() {
     MyApplication.activityResumed();
     super.onResume();
 }
}

here is MyApplication class :

public class MyApplication extends Application  {

public static boolean isActivityVisible() {
    return activityVisible;
}

public static void setVisibilityTrue() {
    activityVisible = true;
}

public static void activityResumed() {
    boolean isScreenOn = true;
    // isScreenOn() method is deprecated API level 21. You should use isInteractive instead:
    PowerManager pm = (PowerManager) MyApplication.getInstance().getSystemService(Context.POWER_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        isScreenOn = pm.isInteractive();
    } else {
        isScreenOn = pm.isScreenOn();
    }
    if (isScreenOn) {
        activityVisible = true;
    }
}

public static void activityPaused() {
    boolean isScreenOn = true;
    PowerManager pm = (PowerManager) MyApplication.getInstance().getSystemService(Context.POWER_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        isScreenOn = pm.isInteractive();
    } else {
        isScreenOn = pm.isScreenOn();
    }
    if (isScreenOn) {
        activityVisible = false;
    }

}
private static boolean activityVisible;

}

when app is in the background we bring it to foreground , with a timer every 30 seconds we check if app screen is locked or not if it is locked we open the app :

private void bringApplicationToFront(Context context) {
    /**
     * check if motitor screen is Off/On
     * */
    boolean isScreenOn = true;
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        isScreenOn = pm.isInteractive();
    } else {
        isScreenOn = pm.isScreenOn();
    }
    if (!isScreenOn) {
        /**
         * check if app  is fore/back ground
         * */
        boolean isInForeBackground = false;
        try {
            isInForeBackground = MyApplication.isActivityVisible();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!isInForeBackground) {
            Intent notificationIntent = new Intent(context, ActivitySplash.class);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
            try {
                pendingIntent.send();
            } catch (PendingIntent.CanceledException e) {
                e.printStackTrace();
            }
        }
    }
}

the tricky part is when app is in the background and screen is locked the onPause() is called after reopening the app (bring it to foreground) means activityVisible is set to false (means not visible which is not true), actually it is correct but it is not what I wanted , to solve this problem I never let the activityVisible to be set when screen is locked and when app is reopened in splash I set the activityVisible to true.

I think if the app comes to foreground its priority is high then it can use gps all the time .

I hope I could explain my solution properly . All the best ...



来源:https://stackoverflow.com/questions/23382561/fused-location-sometimes-stops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!