【问题标题】:Android java.lang.SecurityException with requestLocationUpdates带有 requestLocationUpdates 的 Android java.lang.SecurityException
【发布时间】:2015-09-21 00:36:06
【问题描述】:

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

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) 在 android.os.Parcel.readException(Parcel.java:1499) 在 android.location.ILocationManager$Stub$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) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 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) 在 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) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:300) 在 org.junit.runners.Suite.runChild(Suite.java:128) 在 org.junit.runners.Suite.runChild(Suite.java:24) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:300) 在 org.junit.runner.JUnitCore.run(JUnitCore.java:157) 在 org.junit.runner.JUnitCore.run(JUnitCore.java:136) 在 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)

【问题讨论】:

标签: android location securityexception


【解决方案1】:

如果出现两个不同的错误,您会得到不同的异常。 在没有正确循环器的情况下调用该方法时获得的运行时异常是“错误的线程异常”,您尝试从非 ui 线程或相反的方式执行 UI 工作。安全异常是因为您可能没有正确设置权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 2017-10-15
    • 2019-06-29
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    相关资源
    最近更新 更多