无法在android中模拟被动位置提供程序

时间:2015-08-27 15:42:09

标签: android location

这是我的代码:

public class MockLocationHelper {

    private String providerName ;
    private Context context;
    private LocationManager locationManager ;

    MockLocationHelper(String providerName, Context context){
        this.providerName = providerName;
        this.context = context;

        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        try {
            locationManager.addTestProvider(providerName, true, true, true, false, false, false, false, 1, 2);
            locationManager.setTestProviderEnabled(providerName, true);
        }catch(SecurityException e){
            Log.d(TAG, "SecurityException : MockLocationHelper()");
            e.printStackTrace();
        }catch(IllegalArgumentException e){
            Log.d(TAG, "IllegalArgumentException : MockLocationHelper()");
            e.printStackTrace();
        }
    }

    public void startMocking(String latitude, String longitude) {

        Location location = new Location(providerName);
        location.setLatitude(Double.parseDouble(latitude));
        location.setLongitude(Double.parseDouble(longitude));
        location.setAltitude(0);
        location.setAccuracy(2);
        location.setTime(System.currentTimeMillis());
        location.setElapsedRealtimeNanos(System.nanoTime());

        try {
            locationManager.setTestProviderLocation(providerName, location);
        }catch(SecurityException e){
            Log.d(TAG, "SecurityException : startMocking()");
            e.printStackTrace();
        }catch(IllegalArgumentException e){
            Log.d(TAG, "IllegalArgumentException : startMocking()");
            e.printStackTrace();
        }
    }

    public void stopMocking(){

        try{
            locationManager.removeTestProvider(providerName);
        }catch(SecurityException e){
            Log.d(TAG, "SecurityException : stopMocking()");
            e.printStackTrace();
        }catch(IllegalArgumentException e){
            Log.d(TAG, "IllegalArgumentException : stopMocking()");
            e.printStackTrace();
        }
    }
}

这是onCreate()函数:

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

           final EditText latitude = (EditText) findViewById(R.id.latitude);
           final EditText longitude = (EditText) findViewById(R.id.longitude);

           Location location = new Location(LocationManager.PASSIVE_PROVIDER);
location.setLatitude(Double.parseDouble(latitude.getText().toString()));
location.setLongitude(Double.parseDouble(longitude.getText().toString()))

    final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
              MockLocationHelper  mockLocationHelper = new MockLocationHelper(LocationManager.PASSIVE_PROVIDER, this);
        mockLocationHelper.startMocking(latitude.getText().toString(), longitude.getText().toString());
            mockLocationHelper.stopMocking();

            latitude.setText("");
            longitude.setText("");
        }

运行此代码时,它会提供 IllegalAregumentException:"被动"提供商未知。如果我们用GPS_PROVIDER或NETWORK_PROVIDER替换提供商,此代码可以正常工作。

清单权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>

模拟选项在开发者控制台中启用,我在Kitkat上运行应用程序。为什么我得到这个例外,我应该如何模拟被动位置?

2 个答案:

答案 0 :(得分:1)

您需要先调用addTestProvider。但请注意,你不能模拟被动提供者,你必须嘲笑NETWORK或GPS。

有关addTestProvider行为的详细信息,请参阅this link

    public void test1_TestCaseFoo()
    {
       Location location = new Location("network");
       location.setLatitude(-15.83554363);
       location.setLongitude(-48.01770782);
       location.setTime(new Date().getTime());
       location.setAccuracy(100.0f);
       location.setElapsedRealtimeNanos(System.nanoTime());

       LocationManager locationManager = (LocationManager) getInstrumentation().getTargetContext().getSystemService(Context.LOCATION_SERVICE);
       locationManager.addTestProvider(LocationManager.GPS_PROVIDER, false, false, false, false, true, true, true, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
       locationManager.setTestProviderStatus(LocationManager.GPS_PROVIDER, LocationProvider.AVAILABLE, null, System.currentTimeMillis());
       locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
       locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, location);

       locationManager.addTestProvider(LocationManager.NETWORK_PROVIDER, false, false, false, false, true, true, true, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
       locationManager.setTestProviderStatus(LocationManager.NETWORK_PROVIDER, LocationProvider.AVAILABLE, null, System.currentTimeMillis());
       locationManager.setTestProviderEnabled(LocationManager.NETWORK_PROVIDER, true);
       locationManager.setTestProviderLocation(LocationManager.NETWORK_PROVIDER, location);


       mActivity = getActivity();
       ....

答案 1 :(得分:0)

Android清单文件应如下所示:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />

您可以在 Request User Permissions 中找到更多详细信息,并使用Java代码代替:Location lastKnow = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);如下

try {
    Location lastKnow1 = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
    Location lastKnow2 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Location lastKnow3 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if(lastKnow1 != null){
       --- your code ---
    } if(lastKnow2 != null){
       --- your code ---
    } if(lastKnow3 != null){
       --- your code ---
    } else {
       --- Toast ---
    }
  } catch (Exception e) {
            e.printStackTrace();
          }

使用上述代码,您将永远不会遇到异常问题。 **快乐编码**