带有requestLocationUpdates的Android java.lang.SecurityException

时间:2015-07-03 08:35:26

标签: android location securityexception

目前我尝试为我的Android测试应用程序实现Mock位置,这是我的Mock位置类:

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

这是我的代码,位于测试函数中,用于推送假位置:

MockLocationProvider mock = new MockLocationProvider(LocationManager.NETWORK_PROVIDER, context);
mock.pushLocation(-12.34, 23.45);
LocationManager locMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
LocationListener lis = new LocationListener() {
    public void onLocationChanged(Location location) {
    //You will get the mock location
    }

   @Override
   public void onStatusChanged(String provider, int status, Bundle extras) {

   }

   @Override
   public void onProviderEnabled(String provider) {

   }

   @Override
   public void onProviderDisabled(String provider) {

   }
};
locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, lis, Looper.getMainLooper());

我的问题是requestLocationUpdates函数,如果我不写" Looper.getMainLooper()"我得到一个运行时异常,但现在如果我把这个参数,我有一个安全异常。谁可以帮助我?

例外:

  

java.lang.SecurityException:包名无效:   com.example.myapp.test   android.os.Parcel.readException(Parcel.java:1546)at   android.os.Parcel.readException(Parcel.java:1499)at   android.location.ILocationManager $存根$ Proxy.requestLocationUpdates(ILocationManager.java:582)   在   android.location.LocationManager.requestLocationUpdates(LocationManager.java:867)   在   android.location.LocationManager.requestLocationUpdates(LocationManager.java:490)   在   com.example.myapp.ApplicationTest.testLoc(ApplicationTest.java:220)   在java.lang.reflect.Method.invoke(Native Method)at   java.lang.reflect.Method.invoke(Method.java:372)at   org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:45)   在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)   在   org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)   在   org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)   在   org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)   在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)at   org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)   在   org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)   在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:231)at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:60)at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:50)at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:222)at at   org.junit.runners.ParentRunner.run(ParentRunner.java:300)at at   org.junit.runners.Suite.runChild(Suite.java:128)at   org.junit.runners.Suite.runChild(Suite.java:24)at   org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:231)at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:60)at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:50)at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:222)at at   org.junit.runners.ParentRunner.run(ParentRunner.java:300)at at   org.junit.runner.JUnitCore.run(JUnitCore.java:157)at   org.junit.runner.JUnitCore.run(JUnitCore.java:136)at at   android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)   在   android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:228)   在   android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1853)

1 个答案:

答案 0 :(得分:0)

如果有两个不同的错误,你会得到不同的例外情况。 在没有正确的循环器的情况下调用方法时得到的runtimeexception是一个“错误的线程异常”,你试图从非ui线程做UI工作,或者反过来。安全性异常是因为您可能没有正确设置权限。