当我的Android手机处于移动状态时,我必须对其进行测试,但手机仍处于测试阶段。有没有办法(或应用程序)模拟活动移动?
像假GPS这样的东西,只是假动作。答案 0 :(得分:3)
您可以使用新的模拟器来模拟GPS读数。单击模拟器工具栏上的三个点,单击位置,然后您可以添加要模拟的位置,或提供模拟将遵循的GPX / KML文件
答案 1 :(得分:1)
答案 2 :(得分:1)
正如@xklakoux所述,Android模拟器可让您模拟来自板载设备的测量结果,例如 GPS ,加速度计,陀螺仪等等。
如果您希望在测试期间以编程方式模拟应用程序中的位置,可以将MOCK_LOCATION
权限添加到您的暂存环境AndroidManifest.xml
:
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
然后定义一个模拟的位置服务,可以作为线程任务执行,如下所示:
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import java.util.Random;
/** A class that mocks Location data in Android. */
public final class MockLocationProvider implements Runnable {
/* Member Variables. */
private final String mProviderName;
private final Context mContext;
private final long mBeaconPeriod;
private double mLatitude;
private double mLongitude;
private double mAltitude;
private float mAccuracy;
private boolean mRunning;
/** Constructor. */
public MockLocationProvider(final String pProviderName, final Context pContext, final long pBeaconPeriod) {
// Initialize Member Variables.
this.mProviderName = pProviderName;
this.mContext = pContext;
this.mBeaconPeriod = pBeaconPeriod;
this.mLatitude = 0.0;
this.mLongitude = 0.0;
this.mAltitude = 0.0;
this.mAccuracy = 0.0f;
this.mRunning = false;
}
/** Sets up the Mock Location Provider. */
public final void open() {
// Fetch the LocationManager.
final LocationManager lLocationManager = (LocationManager) this.getContext().getSystemService(Context.LOCATION_SERVICE);
// Register ourself as a TestProdier.
lLocationManager.addTestProvider(this.getProviderName(), false, false, false, false, false, true, true, 0, 5);
// Enable the transmission of mock locations.
lLocationManager.setTestProviderEnabled(this.getProviderName(), true);
// Assert that we're running.
this.mRunning = true;
}
@Override
public void run() {
// Are we still running?
if(this.isRunning()) {
// Publish the current location.
this.publishLocation(this.getLatitude(), this.getLongitude(), this.getAlitude(), this.getAccuracy());
// Sleep for the BeaconPeriod.
try { Thread.sleep(this.getBeaconPeriod()); } catch (final InterruptedException pInterruptedException) { pInterruptedException.printStackTrace(); }
// Run again.
this.run();
}
}
// Update the local parameters. (With a random accuracy between 10 and 15 meters.)
public final void setLocation(final double pLatitude, final double pLongitude, final double pAltitude) throws Exception {
// Allocate a random Accuracy, in meters. (Range: 5m <= n < 15m).
final float lAccuracy = (10.0f * (new Random()).nextFloat()) + 5.0f;
// Update the member variables.
this.setLocation(pLatitude, pLongitude, pAltitude, lAccuracy);
}
// Update the local parameters.
public final void setLocation(final double pLatitude, final double pLongitude, final double pAltitude, final float pAccuracy) throws Exception {
// Update the member variables.
this.mLatitude = pLatitude;
this.mLongitude = pLongitude;
this.mAltitude = pAltitude;
this.mAccuracy = pAccuracy;
// Wait a period.
Thread.sleep(this.getBeaconPeriod());
}
/** Publishes a mock location to the device. */
public final void publishLocation(final double pLatitude, final double pLongitude, final double pAltitude, final float pAccuracyMeters) {
// Fetch the LocationManager.
final LocationManager lLocationManager = (LocationManager) this.getContext().getSystemService(Context.LOCATION_SERVICE);
// Allocate a Location.
final Location lLocation = new Location(this.getProviderName());
// Configure the Location Metrics.
lLocation.setLatitude(pLatitude);
lLocation.setLongitude(pLongitude);
lLocation.setAltitude(pAltitude);
lLocation.setAccuracy(pAccuracyMeters);
lLocation.setElapsedRealtimeNanos(System.nanoTime());
// Use the CurrentTimeMillis as the Location.
lLocation.setTime(System.currentTimeMillis());
// Mock the location.
lLocationManager.setTestProviderLocation(this.getProviderName(), lLocation);
}
/** Closes the down the Mock Provider. */
public final void close() throws Exception {
// Stop running.
this.setRunning(false);
// Wait for the last beacon.
Thread.sleep(this.getBeaconPeriod());
// Fetch the LocationManager.
final LocationManager lLocationManager = (LocationManager) this.getContext().getSystemService(Context.LOCATION_SERVICE);
// Remove ourself as the Test Provider.
lLocationManager.removeTestProvider(this.getProviderName());
}
private final String getProviderName() {
return this.mProviderName;
}
private final Context getContext() {
return this.mContext;
}
private final long getBeaconPeriod() {
return this.mBeaconPeriod;
}
public final double getLongitude() {
return this.mLongitude;
}
public final double getLatitude() {
return this.mLatitude;
}
public final double getAlitude() {
return this.mAltitude;
}
public final float getAccuracy() {
return this.mAccuracy;
}
private final void setRunning(final boolean pIsRunning) {
this.mRunning = pIsRunning;
}
public final boolean isRunning() {
return this.mRunning;
}
}
然后可以使用以下方法对其进行实例化。
new MockLocationProvider(LocationManager.GPS_PROVIDER, mContext, 200L)