Note: This article is translated from Google's official Android developers training document, translator technology generally, because of the love of Android and produced the idea of translation, purely personal interests and interests.
Original link: http://developer.android.com/training/location/location-testing.html
Sample code:
Locationprovider.zip
To test a location-aware app that uses location services, you don't need to move your device from one place to another to generate data. You can put the location service in test mode. In this mode you can send the location object of the test to the locate service and it will be sent to the location client. In test mode, the location service can also activate a geo fence by using the test locations object.
There are several benefits to using the test location:
- The test location allows you to create a specific test data rather than getting some approximate data from the mobile device.
- Since the test location comes from the positioning service, they test every part of your location code. Also, since you can separate test data from your app, you don't need to remove your test code when you publish your app.
- Since you don't need to generate test data from your mobile device, you can test it using the simulator.
The best way to use test locations is to provide an application from an additional test location. This lesson contains a provider app that you can download and use, which you can use to test your app. You can update the provider application appropriately to suit your needs. The relevant content will be explained in the following sections.
The remainder of this lesson will show how to start the test mode and send the test location to the location service using a locating client.
Note:
The test location has no effect on the algorithm used by the location service in behavioral cognition. To learn about behavioral cognition, you can read: Recognizing the User's current Activity (blog link: http://www.cnblogs.com/jdneo/p/3758479.html)
a). Open test Mode
To send the test location to the location service in test mode, a test app must request Access_mock_location permissions. In addition, you must enable the test location on the test device by using the Enablemock locationsoption. To learn how to enable test locations on your device, you can read: Setting up a device for development.
To open the test mode for location services, first connect a locating client to the location service, which has been explained in retrieving the current site (blog link: http://www.cnblogs.com/jdneo/p/3713826.html). Then call Locationclient.setmockmode (True). Once you call this method, the location service closes its internal location provider and sends only the test location that you provided. The following code snippet shows how to call Locationclient.setmockmode (true):
// Define a Locationclient object Public locationclient mlocationclient; ... // Connect to location Services mlocationclient.connect (); ... // When the "location" client is connected, set mock mode Mlocationclinet.setmockmode (true);
Once you have the location client connected to the location service, you must remain connected until you have completed sending the test location. Once you have called Locationclient.disconnect (), the location service will return to its internal location provider. You need to call Locationclient.setmockmode (false) if you want to turn off test mode while locating the client in the connected state.
TWO). Send Test location
Once you have set up the test mode, you can create test location objects and send them to the locate service. The location service then sends these test-location objects to the connected locate client. Location Services also use the test locations object to control the activation of geo-fencing.
To create a new test location, we use your test data to create a new location object. Set the value of the provider to "FLP", and the location service will place it in the emitted position object. The following code snippet shows how to create a new test location:
Private Static FinalString PROVIDER = "FLP"; Private Static Final DoubleLAT = 37.377166; Private Static Final DoubleLNG =-122.086966; Private Static Final floataccuracy = 3.0f; ... /** from input arguments, create a single location with provider set to * "FLP"*/ PublicLocation Createlocation (DoubleLatDoubleLngfloataccuracy) { //Create a new locationLocation NewLocation =NewLocation (PROVIDER); Newlocation.setlatitude (LAT); Newlocation.setlongitude (LNG); Newlocation.setaccuracy (accuracy); returnNewLocation; } ... //Example of creating a new location from test dataLocation testlocation = createlocation (LAT, LNG, accuracy);
In test mode, call Locationclient.setmocklocation () to send a test location to the location service. For example:
Mlocationclient.setmocklocation (testlocation);
Location Services sets the test location as the current location, and the location is emitted as the next updated location. If this new test location crosses a geo fence, the location service activates the GEO fence.
III). Run the test location provider app
This section contains a short overview of the test location provider app, which can be downloaded at the top of the connection, and some guidance on how you should use the app for testing.
Overview
The test location provider app that is involved in this course sends the test location to the location service from a thread in the background that runs the service that was started. By using a service that has been started, the provider app can remain running even if the app's main activity is destroyed due to configuration changes or other system events. By using a background thread, you can have the service perform a long-term test task without blocking the UI thread.
When you open the provider app, the activity that starts allows you to send test parameters to the service and control the type of test you want. The following options are involved:
Pause before test
The time to wait before the provider app sends the test data to the location service. This interval allows you to have time to switch from the provider app to the app you want to test before the test begins.
Send Interval
The intermediate interval at which the provider app sends test data to the location service. This option is further described in the later chapters.
One-time execution
That is, switch from normal mode to test mode, execute a test data, then switch back to normal mode, and kill the service.
Continuous execution
that is, switch from normal mode to test mode, execute the test data indefinitely, then switch back to normal mode, background thread and the started The service is always executed and even the main activity has been destroyed .
Stop testing
If a continuous test is in progress, the option stops it; otherwise, a warning message is returned. The service that is started switches from test mode to normal mode and then aborts itself. The background thread will also be aborted.
In addition to the above options, the provider app has two display states:
Application Status
Displays information about the lifecycle State of the provider app.
Connection Status
Displays connection status information that is connected to the client.
Because the service that is started is running, it also pushes notifications for test status. These notifications allow you to observe status updates even if the app is not in the foreground. When you click on a notification, the main activity of the provider app is returned to the foreground.
testing using the test location provider app
To test the test location data from the test location provider app:
Install the test location provider on a device that has a Google Play service. Location services are part of the Google Play service.
On the device, enable the test location. To learn how to do this, you can view: Setting up a Device for development.
Start the provider app from the launcher, and then select the mode on the main interface.
Unless you remove the start pause, the test location provider app pauses some time and then starts sending the test data to the location service.
Run the app you want to test. When you test the location provider app at run time, the app you're testing sinks receives the test location instead of the real location.
If the provider app is in the middle of a continuous test, you can switch back to the real place by clicking "Stop Test". This forces the started service to turn off Test mode and stops itself. When the service is stopped, the background thread is also destroyed.
IV). Recommendations for testing
The following sections contain recommendations for creating test data and using the data provided by the test location provider app.
Select a Send Interval
Each test location provider that lets location service send locations has its own minimum update interval. For example, a GPS provider cannot send geolocation updates at a frequency that is updated once per second. , and the Wi-Fi location frequency is capped at 5 seconds. For real locations, these cycles are automatically processed, but you'll need to count them when you send the test location again. For example, you should not send a test location at a frequency that is more than once a second. If you are in the test room (relying more on Wi-Fi), you should use a frequency of five seconds.
Simulation Movement Speed
To simulate the speed of an actual device, you can shorten or extend the distance between two consecutive locations. For example, you can move 88 feet per second to simulate a car's driving, as this equates to 60 miles per hour. In contrast, a change of 1.5 feet per second can be simulated faster than walking, which equates to 3 miles per hour.
Calculate location Data
By searching the network, you can find many small programs that calculate the distance between the latitude and longitude of the starting position and the latitude and longitude of the ending position, and the formula used to calculate the distance between the two-point latitude and longitude. The location class also provides two ways to calculate distances between two points.
Distancebetween ()
A static method that calculates the distance between two points based on latitude and longitude.
Distanceto ()
Returns the distance to another location for a given location.
geo-fencing test
When you test a geo-fencing application, the test data used should be able to reflect different forms of action, such as walking, biking, driving, and trains. For slow action mode, you can make the distance between two points smaller. Correspondingly, for the fast mode of action, you can make the distance between two points larger.
Manage test Data
This lesson contains a test location provider application that can test the latitude and longitude and precision in a constant form. You can consider the organization of other test data:
Xml:
Location data can be stored in the corresponding XML file in the test location provider application so that test data can be separated from the code for easy testing.
Download from server:
Store location data on the server and have the provider apply the download. Because the data and applications are completely detached, you can modify the test data without rebuilding the app. You can also modify the test data on the server and let the results immediately react to the test location you are testing.
The recorded data