I would like to share what i have prepared so far and at the same time
ask for assistance and suggestions on the next coding steps needed.
Based from short and basic java and android training and online
resources, I have come up with the following theoretical code that has
the following objectives (theoretical because I didn’t test it yet):
explicitly select the GPS provider (GPS/cell/wifi) to know the phone’s
location
dislays the current location in a textview
connects to a server via 3g
sends the latitude, longitude and timestamp as frequently as possible
to a server
Below is the code that I prepared:
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
}
}
PLEASE HELP
1. Kindly comment if the code above will meet the four objectives
given.
2. HOW CAN I MEET MY 5th objective ----- I want the android
application to start to trigger connecting to server and sending the
latitude and longitude only when the phone (being used in a car) is
within a road area ( say an area 1km x 30m ). It is continuously
listening to its location but will start sending to server once it
enters the area and will continuously send and will stop only once it
exit the area.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en