我想分享我到目前为止所准备的内容,同时就下一步所需的编码步骤寻求帮助和建议。
基于简短的基本java和android培训以及在线资源,我提出了以下具有以下目标的理论代码(理论上因为我还没有测试过):
以下是我准备的代码:
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.TextView;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class GpsActivity extends Activity {
private LocationManager lm;
private LocationListener locationListener;
public static TelephonyManager tm;
public static TextView tv;
public static Socket s;
public static PrintWriter out;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* retrieve a reference to provide access to information about the telephony services on the device
*/
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
setContentView(R.layout.main);
/**
* retrieve a reference to provide access to the system location services
*/
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
/**
* explicitly select the GPS provider, create a set of Criteria and let android choose the best provider available
*/
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = lm.getBestProvider(criteria, true);
/**
* This method takes in four parameters:
provider: The name of the provider with which you register
minTime: The minimum time interval for notifications, in milliseconds.
minDistance: The minimum distance interval for notifications, in meters.
listener: An object whose onLocationChanged() method will be called for each location update.
*/
locationListener = new MyLocationListener();
lm.requestLocationUpdates(provider, 0, 0, locationListener);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("I currently have no Location Data.");
}
/**
* Connects the Android Client to a given server
*
* @param name
* The name of the remote server
* @param port
* Port number to connect to at the remote server.
* @throws IOException
* @throws UnknownHostException
*/
public static void connect(String name, int port)
throws UnknownHostException, IOException
{
s = new Socket(name, port);
out = new PrintWriter(s.getOutputStream(), true);
}
/**
* Sends a string message to the server.
*
* @param msg
* The message to be sent.
* @throws IOException
*/
public static void send(String msg) throws IOException
{
if (!s.isClosed() && msg != null)
{
out.println(msg);
if (msg.contains("CMD_QUIT"))
{
out.close();
s.close();
Log.i("ServerConnection", "Client Disconnected.");
}
}
}
private class MyLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location loc) {
String txt = "Latitude:" + loc.getLatitude() + "/nLongitude:" + loc.getLongitude();
Log.i("GeoLocation", "My current location is:\n " + txt);
tv.setText("My current location is:\n" + txt);
String msg = loc.getLongitude() + "\n" + loc.getLatitude() + "\n"
+ loc.getTime();
try
{
connect("IP address", 27960);
send("CMD_HELLO");
send(msg);
send("CMD_QUIT");
} catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
请帮助
答案 0 :(得分:0)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
android.OS.Handler
对于5,创建两个位置,一个NorthWest位置和一个代表您的盒子的SouthEast位置。在您的onLocationChanged
方法中,将新位置与角落进行比较,以便(l.lat> se.lat&& l.lat< nw.lat)和(l.lon< se。 lon& l.lon> nw.lon)其中“l”是回调的最新位置,“se”是你边界的东南角,“nw”是你的bounder的西北角。如果它符合上述4个条件,则发送到您的服务器。