简体   繁体   English

网络提供商vs Gps提供者模拟位置Android

[英]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. 就像在run函数中看到的那样,它每秒调用一次。 What is the diffrences between Network Provider and Gps provider? 网络提供商和Gps提供商之间的区别是什么? 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. 网络提供商表示位置取决于蜂窝塔和WiFi接入点的可用性。 The position from GPS provider is based on satellites, this one has basically better accuracy. GPS提供商的位置是基于卫星的,这一精度基本上更好。 Almost all apps give greater priority to the positions received by GPS provider. 几乎所有应用程序都将GPS提供商接收到的位置的优先级更高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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