如何模拟Android活动的用户移动?
我必须在android手机处于移动状态时测试它,但手机仍处于测试阶段。是否有一种方法(或应用程序)来模拟活动移动如何模拟Android活动的用户移动?,android,automated-tests,Android,Automated Tests,我必须在android手机处于移动状态时测试它,但手机仍处于测试阶段。是否有一种方法(或应用程序)来模拟活动移动 有点像假GPS,只是它是假动作 是的,下面的一个肯定很好用: 希望这会有所帮助。是的,下面的一个肯定能很好地工作: 希望这会有所帮助。您可以使用新的模拟器来模拟GPS读数。单击emulator工具栏上的三个点,单击location,然后可以添加要模拟的位置,或者提供模拟将遵循的GPX/KML文件。您可以使用新emulator模拟GPS读数。单击emulator工具栏上的三个点,单
有点像假GPS,只是它是假动作 是的,下面的一个肯定很好用:
希望这会有所帮助。是的,下面的一个肯定能很好地工作:
希望这会有所帮助。您可以使用新的模拟器来模拟GPS读数。单击emulator工具栏上的三个点,单击location,然后可以添加要模拟的位置,或者提供模拟将遵循的GPX/KML文件。您可以使用新emulator模拟GPS读数。单击emulator工具栏上的三个点,单击location,然后您可以添加要模拟的位置,或者提供模拟将遵循的GPX/KML文件。如上所述,Android emulator允许您模拟来自车载设备(如GPS、加速计、陀螺仪等)的测量 如果希望在测试期间以编程方式模拟应用程序中的位置,可以将
mock_位置
权限添加到登台环境的AndroidManifest.xml
:
然后定义一个模拟位置服务,该服务可以作为线程任务执行,如下所示:
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;
}
}
导入android.content.Context;
导入android.location.location;
导入android.location.LocationManager;
导入java.util.Random;
/**在Android中模拟位置数据的类*/
公共最终类MockLocationProvider实现Runnable{
/*成员变量*/
私有最终字符串mProviderName;
私有最终上下文mContext;
私人最终长期合同;
私人双重国籍;
私人双重身份;
私人双重虐待;
私人浮动错误;
私有布尔运算;
/**构造器*/
公共MockLocationProvider(最终字符串pProviderName、最终上下文pContext、最终长pBeaconPeriod){
//初始化成员变量。
this.mProviderName=pProviderName;
this.mContext=pContext;
this.mBeaconPeriod=pBeaconPeriod;
这是0.m坡度=0.0;
该长度=0.0;
该值为0.0;
此点精度=0.0f;
this.mRunning=false;
}
/**设置模拟位置提供程序*/
公开最终作废公开(){
//取LocationManager。
final LocationManager lLocationManager=(LocationManager)this.getContext().getSystemService(Context.LOCATION\u服务);
//将自己注册为TestProdier。
lLocationManager.addTestProvider(this.getProviderName(),false,false,false,false,true,true,0,5);
//启用模拟位置的传输。
lLocationManager.setTestProviderEnabled(this.getProviderName(),true);
//断言我们正在运行。
this.mRunning=true;
}
@凌驾
公开募捐{
//我们还在跑吗?
if(this.isRunning()){
//发布当前位置。
this.publishLocation(this.getLatitude()、this.getLatitude()、this.getAlitude()、this.getAccurance());
//在海滩期睡觉。
尝试{Thread.sleep(this.getBeaconPeriod());}catch(final InterruptedException pInterruptedException){pInterruptedException.printStackTrace();}
//再跑一次。
这个。run();
}
}
//更新本地参数。(随机精度在10到15米之间。)
公共最终void setLocation(最终双重陈词滥调、最终双重pLongitude、最终双重pAltitude)抛出异常{
//分配一个以米为单位的随机精度。(范围:5m如上所述,Android仿真器使您能够模拟来自车载设备(如GPS、加速计、陀螺仪等)的测量
如果希望在测试期间以编程方式模拟应用程序中的位置,可以将mock_位置
权限添加到登台环境的AndroidManifest.xml
:
然后定义一个模拟位置服务,该服务可以作为线程任务执行,如下所示:
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;
}
}
导入android.content.Context;
导入android.location.location;
导入android.location.LocationManager;
导入java.util.Random;
/**在Android中模拟位置数据的类*/
公共最终类MockLocationProvider实现Runnable{
/*成员变量*/
私有最终字符串mProviderName;
私有最终上下文mContext;
私人最终长期合同;
私人双重国籍;
私人双重身份;
私人双重虐待;
私人浮动错误;
私有布尔运算;
/**构造器*/
公共MockLocationProvider(最终字符串pProviderName、最终上下文pContext、最终长pBeaconPeriod){
//初始化成员变量。
this.mProviderName=pProviderName;
this.mContext=pContext;
this.mBeaconPeriod=pBeaconPeriod;
这是0.m坡度=0.0;
该长度=0.0;
该值为0.0;
此点精度=0.0f;
this.mRunning=false;
}
/**设置模拟位置提供程序*/
公开最终作废公开(){
//取LocationManager。
final LocationManager lLocationManager=(LocationManager)this.getContext().getSystemService(Context.LOCATION\u服务);
//将自己注册为TestProdier。
lLocationManager.addTestProvider(this.getProviderName(),false,false,false,false,true,true,0,5);
//启用模拟位置的传输。
lLocationManager.setTestProviderEnabled(this.getProviderName(),true);
//断言我们正在运行。
this.mRunning=true;
}
@凌驾
公开募捐{
//我们还在跑吗?
if(this.isRunning()){
//发布当前位置。
this.publishLocation(this.getLatitude()、this.getLatitude()、this.getAlitude()、this.getAccurance());
//在海滩期睡觉。
试试{Thread.sleep