简体   繁体   中英

Network Provider vs Gps provider to mock location android

In my app want to mock my location to a given place. For now, it's call both providers and spoof the location to the location I gave it. It call it once a secods as you see in the run function. What is the diffrences between Network Provider and Gps provider? what should I use? For now I am using them both, and this is my code:

    public boolean        

    startMockLocation(com.quinny898.library.persistentsearch.Location location) {


    if (location != null && location.getLng() == -1000 && location.getLat() == -1000) {
        try {
            PlaceAPI placeApi = new PlaceAPI();
            placeApi.getLatLng(location);
        } catch (Exception e) {

        }
    }
    if (location != null)
        if (location.getLng() != -1000 && location.getLat() != -1000) {
            this.currentLocation = location;
            try {
                if(!hasProvider) {
                    LocationManager lm = (LocationManager) mContext.getSystemService(
                            Context.LOCATION_SERVICE);
                    //    Toast.makeText(mContext, "Lidt: " + lm.getAllProviders(), Toast.LENGTH_SHORT).show();
                    lm.addTestProvider(LocationManager.NETWORK_PROVIDER, false, false, false, false, false,
                            true, true, android.location.Criteria.POWER_LOW, android.location.Criteria.ACCURACY_FINE);
                    lm.setTestProviderEnabled(LocationManager.NETWORK_PROVIDER, true);

                    LocationManager lm2 = (LocationManager) mContext.getSystemService(
                            Context.LOCATION_SERVICE);
                    lm2.addTestProvider(LocationManager.GPS_PROVIDER, false, false, false, false, false,
                            true, true, android.location.Criteria.POWER_LOW, android.location.Criteria.ACCURACY_FINE);
                    lm2.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
                    this.hasProvider = true;
                }
                isWorking = setMockLocation(location);
                setMockLocation2(location);
            }catch (Exception e)
            {
                isWorking = false;
            }
            return isWorking;
        }

    return false;
}

public void run() {
    Thread thread = new Thread() {
        @Override
        public void run() {

                while (true) {
                    try {
                        if (isWorking) {
                            if (currentLocation != null) {
                                setMockLocation(currentLocation);
                                setMockLocation2(currentLocation);
                            }
                        }
                        sleep(1000);
                    } catch (InterruptedException e) {
                      //  e.printStackTrace();
                        Toast.makeText(mContext, e.toString(), Toast.LENGTH_LONG).show();
                    }
                }
        }
    };

    thread.start();
}

/*
    Stop the spoofed location and return to the source location
 */
public void stopMockLocation() {
    if (isWorking) {


        LocationManager lm = (LocationManager) mContext.getSystemService(
                Context.LOCATION_SERVICE);
        lm.removeTestProvider(LocationManager.NETWORK_PROVIDER);
        lm.removeTestProvider(LocationManager.GPS_PROVIDER);
        currentLocation = null;
        this.isWorking = false;
    }
}

private boolean setMockLocation(com.quinny898.library.persistentsearch.Location location) {
    try
    {
    LocationManager lm = (LocationManager)
            mContext.getSystemService(Context.LOCATION_SERVICE);

    android.location.Location newLocation = new android.location.Location(LocationManager.NETWORK_PROVIDER);

    newLocation.setLatitude(location.getLat());
    newLocation.setLongitude(location.getLng());
    newLocation.setAccuracy(500);
    newLocation.setTime(System.currentTimeMillis());
    if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
    }
    lm.setTestProviderLocation(LocationManager.NETWORK_PROVIDER, newLocation);
    } catch (Exception e ) {
        hasProvider = false;
        return false;
    }
    return true;
}

private boolean setMockLocation2(com.quinny898.library.persistentsearch.Location location) {
    try {
        LocationManager lm = (LocationManager)
                mContext.getSystemService(Context.LOCATION_SERVICE);

        android.location.Location newLocation = new android.location.Location(LocationManager.GPS_PROVIDER);

        newLocation.setLatitude(location.getLat());
        newLocation.setLongitude(location.getLng());
        newLocation.setAccuracy(500);
        newLocation.setTime(System.currentTimeMillis());
        if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
        } // todo
        lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, newLocation);
    } catch (Exception e) {
        hasProvider = false;
        return false;
    }

    return true;
}

Is it a good way to call them both?

The Network provider means the position is based on availability of cell tower and WiFi access points. The position from GPS provider is based on satellites, this one has basically better accuracy. Almost all apps give greater priority to the positions received by GPS provider.

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