Okay nochmals danke für jede Hilfestellung.
Habe das Ganze jetzt mit folgendem Code gelöst:
1package com.example.sos;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.HashMap;
6import java.util.List;
7
8import android.app.AlertDialog;
9import android.content.Context;
10import android.content.DialogInterface;
11import android.content.Intent;
12import android.location.Location;
13import android.location.LocationListener;
14import android.location.LocationManager;
15import android.os.Bundle;
16import android.provider.Settings;
17import android.util.Log;
18
19//Einfacher Multiplexer für Location-Zeugs
20public class LocationController implements LocationListener {
21 private static LocationController _shared;
22 private ArrayList<LocationListener> _listeners;
23 private LocationManager _locationManager;
24 private AlertDialog currentDialog;
25
26 public static LocationController sharedController() {
27 if(_shared == null)
28 _shared = new LocationController();
29 return _shared;
30 }
31
32 public LocationController() {
33 _listeners = new ArrayList<LocationListener>();
34 }
35
36 public void setContext(final Context ctx) {
37 assert _locationManager == null;
38 _locationManager = (LocationManager)ctx.getSystemService(Context.LOCATION_SERVICE);
39 String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
40 HashMap<String, String> askFor = new HashMap<String, String>();
41 askFor.put("network", "Netzwerk");
42 askFor.put("gps", "GPS");
43
44 if(provider != null) {
45 String[] active = provider.split(",");
46 for(String activeProvider : active) {
47 askFor.remove(activeProvider);
48 }
49 }
50 if(askFor.size() != 0) {
51 StringBuilder inactiveProviders = new StringBuilder();
52 Collection<String> values = askFor.values();
53 int idx = 0;
54 for(String inactiveProvider : values){
55 inactiveProviders.append(inactiveProvider);
56 if(idx != values.size() - 1)
57 inactiveProviders.append(" & ");
58 idx++;
59 }
60 Log.d("Location", provider+"");
61
62
63 AlertDialog safe = new AlertDialog.Builder(ctx)
64 .setTitle(inactiveProviders.toString()+" aktivieren!")
65 .setPositiveButton("Okay", new DialogInterface.OnClickListener() {
66 public void onClick(DialogInterface dialog, int whichButton) {
67 LocationController.this.dismissCurrent();
68 Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
69 ctx.startActivity(intent);
70 }
71 }).create();
72
73 safe.show();
74 Log.d("fertig","fertig");
75 }
76 }
77
78 public void dismissCurrent() {
79 if(currentDialog == null) return;
80 currentDialog.dismiss();
81 currentDialog = null;
82 }
83
84 public void startUpdating() {
85 assert _locationManager != null;
86 List<String> providers = _locationManager.getAllProviders();
87 for (int i = 0 ; i < providers.size(); i++){
88 _locationManager.requestLocationUpdates(providers.get(i), 1, 1, this);
89 }
90 }
91
92 public void stopUpdating() {
93 _locationManager.removeUpdates(this);
94 }
95
96 public void addListener(LocationListener listener) {
97 _listeners.add(listener);
98 if(listener instanceof Context)
99 this.setContext((Context)listener);
100 }
101
102 public void removeListener(LocationListener listener) {
103 _listeners.remove(listener);
104 }
105
106 public void onLocationChanged(Location location) {
107 Log.d("Location", "Location changed to ("+location.getLatitude()+"|"+location.getLongitude()+") by Provider ");
108 for(LocationListener listener : _listeners) {
109 listener.onLocationChanged(location);
110 }
111 }
112
113 public void onProviderDisabled(String provider) {
114 for(LocationListener listener : _listeners) {
115 listener.onProviderDisabled(provider);
116 }
117 }
118
119 public void onProviderEnabled(String provider) {
120 for(LocationListener listener : _listeners) {
121 listener.onProviderEnabled(provider);
122 }
123 }
124
125 public void onStatusChanged(String provider, int status, Bundle extras) {
126 for(LocationListener listener : _listeners) {
127 listener.onStatusChanged(provider, status, extras);
128 }
129 }
130
131}
In der Klassen, welche die Position bestimmt (ShowLocationActivity) und jener die die Position dann in der Map anzeigt habe ich den LocationController wie folgt in der onCreate-Funktion eingebudnen:
1LocationController.sharedController().addListener(this);
in onResume gibt es folgenden Aufruf:
1LocationController.sharedController().startUpdating();
in onPause jweils folgendes:
1LocationController.sharedController().stopUpdating();
Somit kann ich von beiden Klassen einfach auf Latitute und Longitude zugreifen
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.