Ortungssystem im ASyncTask

S

Sascha_K1994

Stammgast
81
Da ich da eine weile dran saß und es so gemacht habe das es eigentlich jeder nutzen kann möchte ich dieses mal weiter an euch geben.

Es handelt sich um den Quellcode um eine Ortung zu machen.

Geortet werden kann GPS, Mobilfunk und passiv, das genauste (also GPS) wird natürlich bevorzugt.

Weiterhin sind noch kleine tolle Teile drin ;))

---

In den Konstruktor:
Code:
double Latitude;
double Longitude;
String Ortungsprovider = null;

zum aufrufen der Klasse:
Code:
GetPosition getpositiontask = new GetPosition(boolean *OB GPS VERSUCHT WERDEN SOLL*, int *MAX MINUTEN DES ALTEN DATENSATZES*, boolean *OB EIN PROGRESSDIALOG ANGEZEIGT WERDEN SOLL*, boolean *OB DAS PROGRESSDIALOG EIN ABBRECHEN HABEN SOLL*);
getpositiontask.execute();

So und nun die eigentliche Klasse im ASyncTask:
Code:
private class GetPosition extends AsyncTask<Void, Void, Location> implements LocationListener{
    	
        long old_Datensatz;
        private Location location;
        private LocationManager lm;
        private boolean use_gps;
        private boolean ist_gps_aktiv = false;
        private boolean ist_network_aktiv = false;
        private boolean ist_passive_aktiv = false;
        private boolean zeige_progressbar;
        private boolean zeige_progresskill;
        private String useprovider;
        private long minTime = 0;
        private float minDistance = 0;
        
        private GetPosition(boolean benutzeGPS, int minuten, boolean zeigeProgress, boolean zeigeAbbrechen){
        	
        	use_gps = benutzeGPS;
        	old_Datensatz = minuten*60*1000;
        	zeige_progressbar = zeigeProgress;
        	zeige_progresskill = zeigeAbbrechen;
        }

        ProgressDialog progressload = new ProgressDialog(*Activity*.this);
        
        protected void onPreExecute(){
        	
        	lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        	
        	ist_gps_aktiv = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    		ist_network_aktiv = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    		ist_passive_aktiv = lm.isProviderEnabled(LocationManager.PASSIVE_PROVIDER);	
    		
    		if(use_gps && ist_gps_aktiv){
    			lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, this);
    			useprovider = "GPS_PROVIDER";
    			progressload.setMessage("Suche Standortposition via GPS ...");
    		}else if(ist_network_aktiv){
    			lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, minTime, minDistance, this);
    			useprovider = "NETWORK_PROVIDER";
    			progressload.setMessage("Suche Standortposition via Mobilfunknetz ...");
    		}else if(ist_passive_aktiv){
    			lm.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, minTime, minDistance, this);
    			useprovider = "PASSIVE_PROVIDER";
    			progressload.setMessage("Suche Standortposition ...");
    		}
    		
    		Ortungsprovider = useprovider;
    		
    		System.out.println("Benutze \"" + useprovider + "\" zum orten ...");
        	
        	progressload.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        	progressload.setCancelable(false);
        	
        	if(zeige_progresskill){
        		System.out.println("Zeige Abbrechen-Button an ...");
        		progressload.setButton(DialogInterface.BUTTON_NEGATIVE, "Abbrechen", new DialogInterface.OnClickListener() {
        		    public void onClick(DialogInterface dialog, int which) {
        		        dialog.dismiss();
        		        killprogress();
        		    }
        		});
        	}else{
        		System.out.println("Zeige keinen Abbrechen-Button an ...");
        	}
        	
        	if(zeige_progressbar){
        		progressload.show();
        		System.out.println("Zeige ProgressDialog an ...");
        	}else{
        		System.out.println("Zeige keinen ProgressDialog an ...");
        	}

            
        }
        
        private void killprogress(){
	        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            lm.removeUpdates(this);
            Ortungsprovider = null;
            Toast.makeText(getApplicationContext(), "Standortsuche wurde abgebrochen.", Toast.LENGTH_LONG).show();
        }

        protected Location doInBackground(Void... params){
            
        	//Versuche den letzten Standort zu bekommen - spart Akku
            Location lastLocation = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            
            // Wenn der letzte Standort älter als old_Datensatz ist, suche einen neuen
            if ((System.currentTimeMillis() - lastLocation.getTime() > old_Datensatz) || lastLocation == null){
            	
                while (location == null){
                    try {
                    	Thread.sleep(100);
                    }catch (Exception ex) {}
                }
                return location;
            }
            return lastLocation;
        }

        protected void onPostExecute(Location location){
            progressload.dismiss();
            lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            lm.removeUpdates(this);  	
    	Latitude = location.getLatitude();
    	Longitude = location.getLongitude();
    	
    	System.out.println("Latitude: " + Latitude + "\nLongitude: " + Longitude);
        }

        public void onLocationChanged(Location newLocation){
            location = newLocation;
        }

        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
        
    }

Bedanken wäre nett, Anregungen etc sind auch erlaubt :DD
 
  • Danke
Reaktionen: audacity363

Ähnliche Themen

ConfusingFutureGames
  • ConfusingFutureGames
Antworten
6
Aufrufe
1.746
ConfusingFutureGames
ConfusingFutureGames
znieh99
Antworten
2
Aufrufe
2.991
znieh99
znieh99
Fr4gg0r
  • Fr4gg0r
Antworten
1
Aufrufe
4.707
AlexanderW
A
Zurück
Oben Unten