Java tutorial
//package com.java2s; /* * Copyright (c) 2013, Will Szumski * Copyright (c) 2013, Doug Szumski * * This file is part of Cyclismo. * * Cyclismo is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Cyclismo is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Cyclismo. If not, see <http://www.gnu.org/licenses/>. */ import android.util.Log; import java.io.IOException; import java.io.PrintStream; import java.net.Socket; import java.net.UnknownHostException; public class Main { private static final String ANDROID_LOCAL_IP = "10.0.2.2"; public static int emulatorPort = 5554; private static final int PAUSE_DEFAULT = 200; static final double START_LONGITUDE = 51; static final double START_LATITUDE = -1.3f; static final double DELTA_LONGITUDE = 0.0005f; static final double DELTA_LADITUDE = 0.0005f; public static int SHORT_WAIT_TIME = 2000; static boolean isEmulator = true; public static final String LOG_TAG = "MyTracksTest"; /** * Sends Gps data to emulator, and the start value has an offset. * * @param number send times * @param offset is used to compute the start latitude and longitude * @param pause pause interval between each sending */ public static void sendGps(int number, int offset, int pause) { if (number < 1) { return; } int pauseInterval = PAUSE_DEFAULT; if (pause != -1) { pauseInterval = pause; } // If it's a real device, does not send simulated GPS signal. if (!isEmulator) { return; } PrintStream out = null; Socket socket = null; try { socket = new Socket(ANDROID_LOCAL_IP, emulatorPort); out = new PrintStream(socket.getOutputStream()); double longitude = START_LONGITUDE + offset * DELTA_LONGITUDE; double latitude = START_LATITUDE + offset * DELTA_LADITUDE; for (int i = 0; i < number; i++) { out.println("geo fix " + longitude + " " + latitude); longitude += DELTA_LONGITUDE; latitude += DELTA_LADITUDE; Thread.sleep(pauseInterval); } // Wait the GPS signal can be obtained by MyTracks. Thread.sleep(SHORT_WAIT_TIME); } catch (UnknownHostException e) { System.exit(-1); } catch (IOException e) { System.exit(-1); } catch (InterruptedException e) { System.exit(-1); } finally { if (out != null) { out.close(); } } } /** * Send Gps data to emulator. * * @param number number of signals * @param offset is used to compute the start latitude and longitude */ public static void sendGps(int number, int offset) { sendGps(number, offset, -1); } /** * Send Gps data to emulator. * * @param number number of signals */ public static void sendGps(int number) { sendGps(number, 0, -1); } /** * Waits n milliseconds. * * @param milliseconds time to sleep */ public static void sleep(long milliseconds) { try { Thread.sleep(milliseconds); } catch (InterruptedException e) { Log.e(LOG_TAG, "Unable to sleep " + milliseconds, e); } } }