如何在Android中设置自定义/模拟位置?

时间:2013-12-25 17:22:16

标签: java android location

我已经创建了一个显示用户搜索位置的应用程序。现在我想将用户搜索的位置设置为当前位置,以便Android系统假定此特定位置是当前位置。现在设备中安装的任何应用程序(Fb,Twitter,Quora ...等等)都必须将此位置读取为当前位置。如何实现此目的?

这是否符合上述要求?

private void setMockLocation(double latitude,double longitude,float accuracy){

    lm.addTestProvider (LocationManager.GPS_PROVIDER,
            "requiresNetwork" == "",
            "requiresSatellite" == "",
            "requiresCell" == "",
            "hasMonetaryCost" == "",
            "supportsAltitude" == "",
            "supportsSpeed" == "",
            "supportsBearing" == "",
            android.location.Criteria.POWER_LOW,
            android.location.Criteria.ACCURACY_FINE);      

    Location newLocation = new Location(LocationManager.GPS_PROVIDER);

    newLocation.setLatitude(latitude);
    newLocation.setLongitude(longitude);
    newLocation.setAccuracy(accuracy);

    lm.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
    lm.setTestProviderStatus(LocationManager.GPS_PROVIDER,LocationProvider.AVAILABLE,null,System.currentTimeMillis());      
    lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, newLocation);      
}

0 个答案:

没有答案