【问题标题】:Not able to mock passive location provider in android无法在 android 中模拟被动位置提供程序
【发布时间】:2015-08-27 15:42:09
【问题描述】:

这是我的代码:

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 : "passive" provider unknown。 如果我们将 provider 替换为 GPS_PROVIDER 或 NETWORK_PROVIDER,则此代码可以正常工作。

清单权限:

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

开发者控制台中启用了模拟选项,我正在 Kitkat 上运行应用程序。为什么我会收到此异常,我应该如何模拟被动位置?

【问题讨论】:

  • 你能把你的整个错误贴出来

标签: android location


【解决方案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();
       ....

【讨论】:

    【解决方案2】:

    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();
              }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      • 2011-12-12
      • 2012-11-13
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      相关资源
      最近更新 更多