我在下面的代码中获取每分钟的位置。实际上,根据每分钟获取位置的要求,并通过套接字将其发送到服务器。
下面是我的代码,它错过了每分钟服务器上的位置更新,我不知道背后的原因。请建议。
private static final int PERIOD=60000;
private void initLocationTracker()
{
try{
mgr=(AlarmManager)getSystemService(ALARM_SERVICE);
Intent i=new Intent(this, LocationPoller.class);
i.putExtra(LocationPoller.EXTRA_INTENT, new Intent(this, LocationReceiver.class));
i.putExtra(LocationPoller.EXTRA_PROVIDER, LocationManager.GPS_PROVIDER);
pi=PendingIntent.getBroadcast(this, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), PERIOD, pi);
}
catch (Exception e)
{
String s = e.getMessage();
}
}
以下是我的广播接收器和套接字代码
public class LocationReceiver extends BroadcastReceiver {
private String _serverAddress;
private String _addressType;
private int _port;
private long _driverId;
private Location _location;
private String _message;
//private Context _context;
@Override
public void onReceive(Context context, Intent intent) {
File log= new File(Environment.getExternalStorageDirectory(), "LocationLog.txt");
try {
Bundle b=intent.getExtras();
Location loc=(Location)b.get(LocationPoller.EXTRA_LOCATION);
String msg;
if (loc==null) {
loc=(Location)b.get(LocationPoller.EXTRA_LASTKNOWN);
if (loc==null) {
msg=intent.getStringExtra(LocationPoller.EXTRA_ERROR);
}
else {
msg="TIMEOUT, lastKnown="+loc.toString();
}
}
else {
msg=loc.toString();
_location=loc;
getServerAddress(context);
createMessage();
if(!CommonRoutines.isNetworkAvailable(context))
{
return;
}
SocketManager socketManager = new SocketManager();
socketManager.execute();
}
if (msg==null) {
msg="Invalid broadcast received!";
}
}
catch (Exception e) {
Log.e(getClass().getName(), "Exception appending to log file", e);
}
}
private boolean postLocation()
{
boolean result = false;
InetAddress inetAddress=null;
Socket socket=null;
DataOutputStream outputStream = null;
try
{
inetAddress = InetAddress.getByName(X.X.X.X);
socket = new Socket(inetAddress,XXXX);
outputStream= new DataOutputStream(socket.getOutputStream());
outputStream.write(_message.getBytes());
socket.close();
}
catch (UnknownHostException e)
{
String s = e.getMessage();
}
catch (IOException e)
{
String s = e.getMessage();
}
finally
{
if(socket!=null){
try
{
socket.close();
}
catch (IOException e){}
}
if(outputStream!=null)
{
try {
outputStream.close();
}
catch (IOException e){}
}
}
return result;
}
private void createMessage()
{
try{
JsonGeoLocation geoLocation = new JsonGeoLocation();
JsonGeoPosition geoPostion = new JsonGeoPosition();
_message="";
Time now = new Time();
now.setToNow();
geoLocation.setDriverId(_driverId);
geoLocation.setLocationDateTime(DateFormat.getDateTimeInstance().format(new Date()));
geoPostion.setLatitude(_location.getLatitude());
geoPostion.setLongitude(_location.getLongitude());
geoLocation.setGeoPosition(geoPostion);
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
_message = gson.toJson(geoLocation)+"-EOM-";
}
catch (Exception e){
String s = e.getMessage();
}
}
public class SocketManager extends AsyncTask<Void,Integer,Void>
{
@Override
protected Void doInBackground(Void... voids) {
postLocation();
return null;
//return null; //To change body of implemented methods use File | Settings | File Templates.
}
}
}
下面是示例日志
2012-10-23 00:00:48.0135 . Connection request initiated... 2012-10-23 00:00:48.0135 . Connection request accepted... 2012-10-23 00:00:48.0135 . Connection Id: 28383573 2012-10-23 00:00:48.3171 . Entered into OnDataIn ConnectionId: 28383573 2012-10-23 00:00:48.3176 . Data received: {"F0":7,"F2":{"F0":51.57605743408203,"F1":0.09495019912719727},"F1":"Oct 23, 2012 12:00:48 AM"}-EOM- 2012-10-23 00:00:48.3176 . Received: JobId: 7 DateTime: 23/10/2012 00:00:48 Latitude:
51.576057434082 Longitude: 0.0949501991271973 2012-10-23 00:00:48.3966 . Connection disconnected... 2012-10-23 00:57:57.9396 .
2012-10-23 00:57:57.9396 . Connection request initiated... 2012-10-23 00:57:57.9396 . Connection request accepted... 2012-10-23 00:57:57.9396 . Connection Id: 17817267 2012-10-23 00:57:58.2641 . Entered into OnDataIn ConnectionId: 17817267 2012-10-23 00:57:58.2646 . Data received: {"F0":7,"F2":{"F0":51.57625961303711,"F1":0.09462833404541016},"F1":"Oct 23, 2012 12:58:01 AM"}-EOM- 2012-10-23 00:57:58.2646 . Received: JobId: 7 DateTime: 23/10/2012 00:58:01 Latitude:
51.5762596130371 Longitude: 0.0946283340454102 2012-10-23 00:57:58.3396 . Connection disconnected... 2012-10-23 00:58:55.4589 .