B
Braesident
Ambitioniertes Mitglied
- 1
Mein Versuch GPS Daten aus einem Service heraus zu laden schlägt leider immer fehl.
dieser Betrifft die Zeilen:
lmGPS.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,llGPS);
lmNET.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,llNET);
in folgendem Code
Wie muss die Abfrage sonst gestaltet werden?
Code:
Fehler beim GPS auslesen: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Fehler beim NET auslesen: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
lmGPS.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,llGPS);
lmNET.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,llNET);
in folgendem Code
Code:
public class MessagingService extends FirebaseMessagingService {
String locationAdress = "";
SharedPreferences sp;
@Override
public void onMessageReceived( RemoteMessage remoteMessage ) {
super.onMessageReceived(remoteMessage )
sp = PreferenceManager.getDefaultSharedPreferences(MessagingService.this);
String action = sharedPreferences.getString(
getString( R.string.preference_action_key ) ,
getString( R.string.preference_action_default));
if ( remoteMessage.getFrom().equals( meineMsgID ) ){
if ( action.equals("send") ){
LocationManager lmGPS = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener llGPS = new myGPSLocationListener();
try{
lmGPS.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,llGPS);
} catch (Exception e){ Log.e( "FB_MSG_SRV" , "Fehler beim GPS auslesen: " + e); }
if ( locationAdress == "" ){
LocationManager lmNET = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener llNET = new myNETLocationListener();
try{
lmNET.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,llNET);
} catch (Exception e){ Log.e( "FB_MSG_SRV" , "Fehler beim NET auslesen: " + e); }
}
sendToServer( locationAdress );
}
}
}
class myGPSLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
if(location != null){
double pLat = location.getLatitude();
double pLong = location.getLongitude();
if ( "" + pLat != "" && "" + pLong != "" ){
int grad = (int) pLat;
double tmp = (pLat - grad) * 60;
int min = (int) tmp;
double sek = (tmp - min) * 60;
locationAdress = Integer.toString(grad) + "°"
+ Integer.toString(min) + "'"
+ Double.toString(roundOn(sek,3))
+ "\"" + iif( grad >= 0 , "N" , "S" );
grad = (int) pLong;
tmp = (pLong - grad) * 60;
min = (int) tmp;
sek = (tmp - min) * 60;
locationAdress = locationAdress + " "
+ Integer.toString(grad)
+ "°" + Integer.toString(min) + "'"
+ Double.toString(roundOn(sek,3))
+ "\"" + iif( grad >= 0 , "E" , "W" );
}
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
class myNETLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
if(location != null){
double pLat = location.getLatitude();
double pLong = location.getLongitude();
int grad = (int) pLat;
double tmp = (pLat - grad) * 60;
int min = (int) tmp;
double sek = (tmp - min) * 60;
locationAdress = Integer.toString(grad) + "°" + Integer.toString(min) + "'" + Double.toString(roundOn(sek,3)) + "\"" + iif( grad >= 0 , "N" , "S" );
grad = (int) pLong;
tmp = (pLong - grad) * 60;
min = (int) tmp;
sek = (tmp - min) * 60;
locationAdress = locationAdress + "+" + Integer.toString(grad) + "°" + Integer.toString(min) + "'" + Double.toString(roundOn(sek,3)) + "\"" + iif( grad >= 0 , "E" , "W" );
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
public String iif( boolean frage , String p1 , String p2 ){
if (frage=true)
return p1;
return p2;
}
public double roundOn( double zahl , int n ){
double faktor = 1;
for ( int i = 1 ; i <= n ; i++ ){
faktor = faktor * 10;
}
return Math.round(zahl*faktor)/faktor;
}
public void sendToServer( final String text ){
new Thread(new Runnable() {
@Override
public void run() {
try {
// ...
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
Wie muss die Abfrage sonst gestaltet werden?