zoukankan      html  css  js  c++  java
  • Android中的ACCESS_MOCK_LOCATION权限使用Demo

    转载地址:http://mobiarch.wordpress.com/2012/07/17/testing-with-mock-location-data-in-android/

    The DDMS tool can be used to push out test location during testing. However, it has two serious limitations:

     

    1. DDMS sets location for GPS location provider only. If your application uses network provider then you are out of luck.
    2. DDMS can set location for an emulator device only. You can not do testing using a real device.

     

    If you need to test with real device or using the network location provider, you will need to develop a mock provider within the application. A mock provider can represent any location provider – network or GPS. Writing a mock provider itself is easy. Just be careful about removing the feature before publishing your application.

     

    In this article, we will see how to create a mock location provider.

     

    First, we will develop a class that will encapsulate the details:

    public class MockLocationProvider {
      String providerName;
      Context ctx;
     
      public MockLocationProvider(String name, Context ctx) {
        this.providerName = name;
        this.ctx = ctx;
     
        LocationManager lm = (LocationManager) ctx.getSystemService(
          Context.LOCATION_SERVICE);
        lm.addTestProvider(providerName, false, false, false, false, false, 
          true, true, 0, 5);
        lm.setTestProviderEnabled(providerName, true);
      }
     
      public void pushLocation(double lat, double lon) {
        LocationManager lm = (LocationManager) ctx.getSystemService(
          Context.LOCATION_SERVICE);
     
        Location mockLocation = new Location(providerName);
        mockLocation.setLatitude(lat);
        mockLocation.setLongitude(lon); 
        mockLocation.setAltitude(0); 
        mockLocation.setTime(System.currentTimeMillis()); 
        lm.setTestProviderLocation(providerName, mockLocation);
      }
     
      public void shutdown() {
        LocationManager lm = (LocationManager) ctx.getSystemService(
          Context.LOCATION_SERVICE);
        lm.removeTestProvider(providerName);
      }
    }

    A brief description of the MockLocationProvider class may be helpful:

    • The constructor takes the name of the location provider that this mock provider will replace. For example, LocationManager.GPS_PROVIDER. The calls to the addTestProvider() and setTestProviderEnabled() tells the LocationManager that the given provider will be replaced by mock data.
    • The pushLocation() method supplies mock location data for a given provider.

    Any activity or service can easily use the class. Here, we are replacing the network provider with a mock one.

    public class MainActivity extends Activity {
      MockLocationProvider mock;
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     
        mock = new MockLocationProvider(LocationManager.NETWORK_PROVIDER, this);
     
        //Set test location
        mock.pushLocation(-12.34, 23.45);
     
        LocationManager locMgr = (LocationManager) 
           getSystemService(LOCATION_SERVICE);
        LocationListener lis = new LocationListener() {
          public void onLocationChanged(Location location) {
              //You will get the mock location
          }
          //...
        };
     
        locMgr.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 1000, 1, lis);
      }
     
      protected void onDestroy() {
       mock.shutdown();
       super.onDestroy();
      }
    }

    Setting up Security

    For mock location to work, certain permissions have to be set.

    You will need to request the android.permission.ACCESS_MOCK_LOCATION permission, in addition to any other permission.

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

    Finally, you will need to enable mock locations for the device using Settings > Developer options > Allow mock locations.

    Disabling Mock Location

    It is important that you hide the mock location provider business from the release build. A good way to do that is to enable mock location only if the application is being run in debug mode. In your code, check if debuggable flag is set:

    if ((getApplication().getApplicationInfo().flags & 
        ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
        mock = new MockLocationProvider(
            LocationManager.NETWORK_PROVIDER, this);
     
        //Set test location
        mock.pushLocation(-12.34, 23.45);
    }

    When you test the app using USB debugging or using the emulator, the debug flag is set automatically.

  • 相关阅读:
    Android JNI的使用方法
    Android4.4中jni的native的方法无法找到的解决方案
    如何解决Your project contains C++ files but it is not using a supported native build system
    android-studio开发NDK错误记录:bash: ../../build/intermediates/classes/debug: is a directory
    Android Studio 编译: Program type already present: XXX 解决方案
    ScriptEngineManager is not available in Android and causes a NoClassDefFoundError
    Android 机器人项目Pepper_Android
    Android 优秀开源项目
    准备开发一个运行在Android上的JavaME模拟器
    上周热点回顾(2.3-2.9)团队
  • 原文地址:https://www.cnblogs.com/qinghuaideren/p/3860972.html
Copyright © 2011-2022 走看看