我们正在开发Android上的物流应用。应用程序将启动独立的GPS服务,该服务将更改位置发送到Web服务器(Apache)。 GPS服务以UID开始,Android Studio日志显示GPS服务具有此UID工作。问题是lat和long值正确地来到服务器但UID很简单“0”。我们如何使GPS服务正确地将UID发送到服务器?
这是现在正在运行的代码:
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import java.util.HashMap;
public class MyService extends Service
{
private static final String TAG = "BOOMBOOMTESTGPS";
private LocationManager mLocationManager = null;
private static String UID = null;
private static final int LOCATION_INTERVAL = 5000;
private static final float LOCATION_DISTANCE = 10f; // 10f
private class LocationListener implements android.location.LocationListener // private // public
{
Location mLastLocation;
//public String UID;
public LocationListener(String provider)
{
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location)
{
Log.e(TAG, "onLocationChanged: " + location);
mLastLocation.set(location);
//Intent intent = new Intent(this, MyService.class);
HashMap<String, String> data = new HashMap<String, String>();
data.put("id", UID); // SIIN SOOVIN KÄTTE SAADA VÄÄRTUST!!!!
//data.put("id2", iddata);
data.put("lat", Double.toString(location.getLatitude()));
data.put("lng", Double.toString(location.getLongitude()));
AsyncHttpPost asyncHttpPost = new AsyncHttpPost(data);
asyncHttpPost.setListener(new AsyncHttpPost.Listener(){
@Override
public void onResult(String result) {
// do something, using return value from network
}
});
asyncHttpPost.execute("https://www.isekallur.ee/android/read_gps2.php");
Log.e(TAG, "DATA: " + data);
}
// private void postText(){
//}
@Override
public void onProviderDisabled(String provider)
{
Log.e(TAG, "onProviderDisabled: " + provider);
}
@Override
public void onProviderEnabled(String provider)
{
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.e(TAG, "onStatusChanged: " + provider);
}
}
LocationListener[] mLocationListeners = new LocationListener[] {
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
@Override
public IBinder onBind(Intent intent)
{
return null;
}
/*@Override
protected void onHandleIntent(Intent intent) {
String myID = intent.getExtras().getString("USER_ID");
}*/
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
String iddata =(String) intent.getExtras().get("USER_ID");
final String UID = intent.getStringExtra("USER_ID");
//doSomething2(UID);
Log.e(TAG, "UID: " + UID + " - " + iddata);
//postID(UID);
return START_STICKY; // START_STICKY_COMPATIBILITY // START_STICKY
}
/*private void postID(String UID){
Log.e(TAG, "UID2: " + UID);
}*/
@Override
public void onCreate()
{
Log.e(TAG, "onCreate");
initializeLocationManager();
try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
@Override
public void onDestroy()
{
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
}