Testing GPS in Android

后端 未结 6 594
-上瘾入骨i
-上瘾入骨i 2020-12-07 23:38

How do you test GPS applications in Android? Can we test it using the Android emulator?

6条回答
  •  难免孤独
    2020-12-07 23:52

    Maybe you want to a use a Unit test, so you can try this:

    public class GPSPollingServiceTest extends AndroidTestCase {
        private LocationManager locationManager;
    
        public void testGPS() {
            LocationManager locationManager = (LocationManager) this.getContext().getSystemService(Context.LOCATION_SERVICE);
            locationManager.addTestProvider("Test", false, false, false, false, false, false, false, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
            locationManager.setTestProviderEnabled("Test", true);
    
            // Set up your test
    
            Location location = new Location("Test");
            location.setLatitude(10.0);
            location.setLongitude(20.0);
            locationManager.setTestProviderLocation("Test", location);
    
            // Check if your listener reacted the right way
    
            locationManager.removeTestProvider("Test");
        }
    }
    


    For this to work you also need a required permission to the AndroidManifest.xml:

    
    

提交回复
热议问题