android - 持续向 Android 模拟器发送 GPS 数据

标签 android android-emulator geolocation gps

我正在为 Android 编写一个应用程序,该应用程序将使用 GPS 接收器在 Android 设备移动时测量速度。我知道我可以远程登录到 Android 模拟器并使用“geo fix”命令发送地理坐标。不过我想知道的是,有没有办法持续发送地理定位数据来模拟汽车在街上行驶?否则,我还能如何测试我的应用程序?

最佳答案

嗨,在我的第一个基于 GPS 的项目中,我遇到了同样的问题,所以我编写了一个 java 代码,将 GPS Fix 发送到我的 android 模拟器,下面是代码 fragment

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class Main {

    static final int PAUSE = 4000; // ms
    static final float START_LONGITUDE = 51, START_LATITUDE = -1.3f;
    static final int NO_SAMPLE = 100;
    static final float DELTA_LONGITUDE = 0.000005f, DELTA_LADITUDE = 0.000005f;

    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost", 5554); // usually 5554

            PrintStream out = new PrintStream(socket.getOutputStream());
            float longitude = START_LONGITUDE, latitude = START_LATITUDE;
            String str;

            for (int i = 0; i < NO_SAMPLE; i++) {
                str = "geo fix " + latitude + " " + longitude + "\n";
                out.print(str);
                System.out.print(str);

                Thread.sleep(PAUSE);

                longitude += DELTA_LONGITUDE;
                latitude += DELTA_LADITUDE;
            }
        } catch (UnknownHostException e) {
            System.exit(-1);
        } catch (IOException e) {
            System.exit(-1);
        } catch (InterruptedException e) {
            System.exit(-1);
        }

    }
}

关于android - 持续向 Android 模拟器发送 GPS 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6026113/

相关文章:

java - android 读取联系人的问题

Android 模拟器启动后自动关闭

geolocation - 是否可以使用 PubNub 进行类似 uber 的 map 跟踪?

linux - 我在哪里? (地理位置,Emacs和Perl)

javascript - 为什么我的 https 来源没有使用 navigator Geolocation 服务的权限?

Android线程等待直到可见

android - 如何在不提示用户的情况下向 Android 应用程序授予运行时权限

java - Android: Volley HTTP 补丁请求

android - 从端点类中的实体中选择几个属性

Android - 获取自定义对话框中组件的句柄