S
SlayNox
Ambitioniertes Mitglied
- 15
Hallo liebes Forum,
mein bisherigen suchen ergaben leider keine treffer, deshalb schreibe ich ein neues Thema.
Ich benötige eine Zuverlässige GPS abfrage. Ich bin in einem Abschleppdienst tätig, indem die Fahrer meine App auf dem Handy haben. Diese wird unter anderem für die Auftragsverwaltung und noch ein paar mehr sachen verwendet. Momentan beziehen wir unsere Standortabfragen der Fahrer/Fahrzeuge über das eingebaute TomTom System. Dies soll sich aber in Zukunft ändern. Ich habe mich lange an der GPS abfrage veruscht und bis zu Version 8.0.0 hat es auch relativ zuverlässig funktioniert. Nun jedoch habe ich Probleme.
Ziel ist es die Standortabfragen in regelmäßigen Abständen auf einen Webserver zu laden. Dies muss zuverlässig funktionieren. Villeicht kann mir der ein oder andere Tipps geben, dies zu gewährleisten.
Mein aktueller Code sieht wie folgt aus:
Startbefehl:
GPS Tracker Klasse:
HTTP Upload:
(Bitte hier vom "UploadImage" nicht irritieren lassen.
Ich hoffe mir kann jemand helfen wie meine GPS abfragen (selbstverändlich wird die Internetanbindung außer Acht gelassen) zuverlässig ankommen
Liebe Grüße
mein bisherigen suchen ergaben leider keine treffer, deshalb schreibe ich ein neues Thema.
Ich benötige eine Zuverlässige GPS abfrage. Ich bin in einem Abschleppdienst tätig, indem die Fahrer meine App auf dem Handy haben. Diese wird unter anderem für die Auftragsverwaltung und noch ein paar mehr sachen verwendet. Momentan beziehen wir unsere Standortabfragen der Fahrer/Fahrzeuge über das eingebaute TomTom System. Dies soll sich aber in Zukunft ändern. Ich habe mich lange an der GPS abfrage veruscht und bis zu Version 8.0.0 hat es auch relativ zuverlässig funktioniert. Nun jedoch habe ich Probleme.
Ziel ist es die Standortabfragen in regelmäßigen Abständen auf einen Webserver zu laden. Dies muss zuverlässig funktionieren. Villeicht kann mir der ein oder andere Tipps geben, dies zu gewährleisten.
Mein aktueller Code sieht wie folgt aus:
Startbefehl:
Code:
private void startGPSService(){
context.startForegroundService(new Intent(context, GPSTracker.class));
}
GPS Tracker Klasse:
Code:
public class GPSTracker extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
IBinder mBinder = new LocalBinder();
private GoogleApiClient mGoogleApiClient;
private PowerManager.WakeLock mWakeLock;
private LocationRequest mLocationRequest;
// Flag that indicates if a request is underway.
private boolean mInProgress;
private static Location mLocation;
private Boolean servicesAvailable = false;
private static Context mContext;
public class LocalBinder extends Binder {
public GPSTracker getServerInstance() {
return GPSTracker.this;
}
}
@Override
public void onCreate() {
super.onCreate();
startForeground(2,new Notification());
Log.i("GPSTracker", "Service gestartet");
mContext = getApplicationContext();
mInProgress = false;
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(GPSTracker_Parameter.UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(GPSTracker_Parameter.FASTEST_INTERVAL);
servicesAvailable = servicesConnected();
setUpLocationClientIfNeeded();
}
protected synchronized void buildGoogleApiClient() {
this.mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
private boolean servicesConnected() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == resultCode) {
return true;
} else {
return false;
}
}
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
PowerManager mgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (this.mWakeLock == null) { //**Added this
this.mWakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
}
if (!this.mWakeLock.isHeld()) { //**Added this
this.mWakeLock.acquire();
}
if (!servicesAvailable || mGoogleApiClient.isConnected() || mInProgress)
return START_STICKY;
setUpLocationClientIfNeeded();
if (!mGoogleApiClient.isConnected() || !mGoogleApiClient.isConnecting() && !mInProgress) {
mInProgress = true;
mGoogleApiClient.connect();
}
return START_STICKY;
}
private void setUpLocationClientIfNeeded() {
if (mGoogleApiClient == null)
buildGoogleApiClient();
}
@Override
public void onLocationChanged(Location location) {
mLocation = location;
createLocationFile();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public String getTime() {
SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return mDateFormat.format(new Date());
}
@Override
public void onDestroy() {
this.mInProgress = false;
if (this.servicesAvailable && this.mGoogleApiClient != null) {
this.mGoogleApiClient.unregisterConnectionCallbacks(this);
this.mGoogleApiClient.unregisterConnectionFailedListener(this);
this.mGoogleApiClient.disconnect();
this.mGoogleApiClient = null;
}
if (this.mWakeLock != null) {
this.mWakeLock.release();
this.mWakeLock = null;
}
Log.i("GPSTracker", "Service wurde gestoppt");
super.onDestroy();
}
@Override
public void onConnected(Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(this.mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
mInProgress = false;
mGoogleApiClient = null;
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
mInProgress = false;
if (connectionResult.hasResolution()) {
// If no resolution is available, display an error dialog
} else {
}
}
private static List<Address> getGeocodeAdress(Location location) {
Geocoder geocoder = null;
List<Address> addresses = null;
try {
geocoder = new Geocoder(mContext, Locale.getDefault());
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
public static double getLatitude() {
return mLocation.getLatitude();
}
public static double getLongitude() {
return mLocation.getLongitude();
}
public static String getLocationAddress() {
String result = "n/A";
if (mLocation != null) {
result = getGeocodeAdress(mLocation).get(0).getAddressLine(0);
}
return result;
}
public static String getLocationCity() {
String result = "n/A";
if (mLocation != null) {
result = getGeocodeAdress(mLocation).get(0).getLocality();
}
return result;
}
public static String getLocationState() {
String result = "n/A";
if (mLocation != null) {
result = getGeocodeAdress(mLocation).get(0).getAdminArea();
}
return result;
}
public static String getLocationCountry() {
String result = "n/A";
if (mLocation != null) {
result = getGeocodeAdress(mLocation).get(0).getCountryName();
}
return result;
}
public static String getLocationPostalCode() {
String result = "n/A";
if (mLocation != null) {
result = getGeocodeAdress(mLocation).get(0).getPostalCode();
}
return result;
}
public static String getLocationStreet() {
String result = "n/A";
if (mLocation != null) {
result = getGeocodeAdress(mLocation).get(0).getThoroughfare();
}
return result;
}
public static String getLocationStreetNumber() {
String result = "n/A";
if (mLocation != null) {
result = getGeocodeAdress(mLocation).get(0).getSubThoroughfare();
}
return result;
}
public static String getLocationCountryCode() {
String result = "n/A";
if (mLocation != null) {
result = getGeocodeAdress(mLocation).get(0).getLocale().getCountry();
}
return result;
}
public static String getCustomAdressStringShort() {
return getLocationStreet() + " " + getLocationStreetNumber() + ", " + getLocationPostalCode() + " " + getLocationCity();
}
public static String getCustomAdressString() {
return getLocationStreet() + " " + getLocationStreetNumber() + ", " + getLocationPostalCode() + " " + getLocationCity() + ", " + getLocationState() + ", " + getLocationCountry() + ", " + getLocationCountryCode();
}
HTTP Upload:
(Bitte hier vom "UploadImage" nicht irritieren lassen.
Code:
public class HTTP_GPS_Upload {
private static boolean inUploadProgress = false;
private static Context context;
public HTTP_GPS_Upload(Context context) {
this.context = context;
if (!inUploadProgress) {
uploadImage();
}
}
public void uploadImage() {
if (!Connectivity.isConnected(context)) {
Log.e("HTTP_GPS_Upload", "Es besteht keine Internetverbindung");
return;
}
Thread t = new Thread(new Runnable() {
@Override
public void run() {
inUploadProgress = true;
String filepath = Parameter.FilePath.EXTERNAL_STORAGE_GPS + User.getIMEI(context) + ".xml";
File file = new File(filepath);
if (!file.exists()) {
return;
}
String requestUrl = new Parameter.ConnectionUrl().GET_CURRENT_HTTPS_SERVER_ADRESS_GPS_UPLOAD(context);
OkHttpClient.Builder client = new HTTP_Trust_Client().getUnsafeOkHttpClient().newBuilder();
File f = new File(filepath);
RequestBody file_body = RequestBody.create(MediaType.parse(filepath), f);
RequestBody request_body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("type", filepath)
.addFormDataPart("uploaded_file", User.getIMEI(context) + ".xml", file_body)
.build();
Request request = new Request.Builder()
.addHeader("Authorization", Credentials.basic(new Parameter.ConnectionUrl().GET_CURRENT_HTTPS_SERVER_ADRESS_USERNAME(context),
new Parameter.ConnectionUrl().GET_CURRENT_HTTPS_SERVER_ADRESS_PASSWORD(context)))
.url(requestUrl)
.post(request_body)
.build();
OkHttpClient requestClient = client.build();
try {
Response response = requestClient.newCall(request).execute();
if (!response.isSuccessful()) {
Log.e("HTTP_GPS_Upload", "Fehler beim upload der Standortdaten");
inUploadProgress = false;
Log.e("RESPONSE", response.body().string());
} else {
inUploadProgress = false;
//Log.e("HTTP_GPS_Upload", "Standortdaten hochgeladen");
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
}
Ich hoffe mir kann jemand helfen wie meine GPS abfragen (selbstverändlich wird die Internetanbindung außer Acht gelassen) zuverlässig ankommen
Liebe Grüße