简体   繁体   中英

Windows Phone 8.1 Recording GPS behind the lock screen

First things first I want to call out that the app I am creating is a Windows Phone 8.1 RT Store Application (not a Silverlight application)

I am currently creating an application as part of my final year project for university as part of a team. The problem I am hitting is that for part of the application I need the ability to record an area on a map. Ie. The user walks around a specific area and we store that area for use in other parts of the application.

I currently am able to use the GPS to record the location and am able to display that accurately on a map. The area that is causing me issues is that the screen needs to be unlocked and the screen on for it to work. As soon as the phone locks the app (obviously) suspends and I am unable to record the location.

I have been looking online and have seen that the previous method for allowing an app to run behind the lock screen has been removed with Windows Phone 8.1, so my question to you is has anyone found any other ways?

Here is the code that I currently have.. I am using a dispatch timer to record the GPS every 20 seconds and then I add that location to a list that I have.

code:

    public void getLoc()
    {


        Debug.WriteLine("Start");
        startButtonEnabled = false;
        stopButtonEnabled = true;


        FindGPS();


        dispatcherTimer.Tick += FindGPS_Event;
        dispatcherTimer.Interval = new TimeSpan(0, 0, 20);
        dispatcherTimer.Start();
    }


    private void FindGPS_Event(object sender, object o)
    {
        FindGPS();
    }
    private async Task FindGPS()
    {
        Debug.WriteLine("Find GPS");
        try
        {
            Geoposition position = await geolocator.GetGeopositionAsync();
            location.Longitude = position.Coordinate.Point.Position.Longitude;
            location.Latitude = position.Coordinate.Point.Position.Latitude;
            location.Accuracy = position.Coordinate.Accuracy;
            location.GeoLocation =
                new Geopoint(new BasicGeoposition() {Latitude = location.Latitude, Longitude = location.Longitude});
            zoomLevel = 18;
        }
        catch (UnauthorizedAccessException)
        {
            Debug.WriteLine("No Data");
        }
        catch (TaskCanceledException)
        {
            Debug.WriteLine("Cancelled");
        }
        finally
        {
            Pushpin icon = new Pushpin()
            {
                Point =
                    new Geopoint(new BasicGeoposition()
                    {
                        Latitude = location.Latitude,
                        Longitude = location.Longitude
                    }),
                Name = "Location"
            };
            location.IconOnMap.Add(icon);
        }
    }

getLoc is called on a button press, and I have another button that stops the dispatchTimer . The timer is needed, but the time frame is currently set for testing purposes.

When the application is suspended, all times are also stopped, also the application is not getting any CPU time at all, which means no code can execute.

If you want to do processing while the phone is locked, you have to implement a background task:

In Windows Phone 8.1 there is a new Geofencing API, which allows you to define regions of interest and the background task will be called when the phone enters or leaves those regions.

Also, you can make the background task to be invoked by a timer (every 15 minutes) and then record the current position.

Lastly, you can use Nokia Sensor Core SDK: http://developer.nokia.com/resources/library/Lumia/sensorcore-sdk.html . It may provide exactly what you want, but it will work only on Lumia phones.

It sounds like you just want to prevent the lock screen from coming up when your app is running. Here is another post that tells you how to do this (look down a few entries for the 8.1 RT app): Prevent automatic screen lock on Windows Phone 8

If you really do want it collecting information when it is NOT the foreground app, one way of handling this is to do a Jogy said and create a background task that runs on a timer. But you won't be able to do it every 20 seconds, once every 15 minutes is about as good as you can get unless you have a SensorCore phone. If you get a SensorCore phone (The latest Nokia/Microsoft phones), with the latest updates to firmware (check with the carrier to make sure you have the latest Nokia released firmware) you can get updates up to every 30 seconds even in the background.

SensorCore also will store the last 10 days of information so the first time your app starts up it has a history. But be aware that you aren't requesting updates every 30 seconds, you get them if somebody else requested them (ie a driving app or something), it just lets you access the locations that were requested by some application. See the SDK documentation here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM