目前,我試圖實現模擬位置我的Android測試應用程序的Android java.lang.SecurityException異常,這是我的模擬位置信息類:與requestLocationUpdates
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)在 機器人。 os.Parcel.readException(Parcel.java:1499)在 android.location.ILocationManager $存根$ Proxy.requestLocationUpdates(ILocationManager.java:582) 在 android.location.LocationManager.requestLocationUpdates(LocationManager.java:867) 在 android.location.LocationManager.requestLocationUpdates(Location (java.lang.reflect.Method.invoke(Native Method)) java.lang.reflect.Method。 invoke(Method.java:372)at org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:45) at 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) 在 有機.junit.internal.runners.statements.RunBef ores.evaluate(RunBefores.java:28) 在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 在 有機.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:231)at org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java :60) org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)at org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:50)at org.junit.runners.ParentRunner $ 2 .evaluate(ParentRunner.java:222)at org.junit.runners.ParentRunner.run(ParentRunner.java:300)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)在 org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:60)處 org.junit.runners.ParentRunner org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 。訪問$在 000(ParentRunner.java:50)在 org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:222)在 org.junit.runners.ParentRunner.run(ParentRunner.java:300)組織.junit.runner.JUnitCore.run(JUnitCore.java:157)at org.junit.runner.JUnitCore.run(JUnitCore.java:136)at android.support.test.internal.runner.TestExecutor.execu TE(TestExecutor。Java的:54) 在 android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:228) 在 android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1853)
您是否在清單中定義了所需的權限?你也應該看看http://stackoverflow.com/questions/20474761/java-lang-securityexception-invalid-package-name-com-google-android-gms – Kalem
在我的情況下,錯誤必須與上下文(我正在傳遞測試上下文,而不是目標上下文)。請參閱https://stackoverflow.com/a/35190090/1621402 – FacundoJ