【发布时间】: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)
【问题讨论】:
-
您是否在清单上定义了所需的权限?你也应该看看stackoverflow.com/questions/20474761/…
-
就我而言,错误与上下文有关(我传递的是测试上下文,而不是目标上下文)。见stackoverflow.com/a/35190090/1621402
标签: android location securityexception