GPS in einem Service abfragen

Ginso89

Mitglied
Hallo,
Ich versuche in einem Service, die aktuellen GPS-Coordinaten zu bekommen:

Code:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
        && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000, 10, new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
    ....

der permission check klappt, die requestLocationUpdates wird aufgerufen, aber die onLocationChanged nicht(und auch keine der anderen 3).
Wenn ich das ganze in der MainActivity mache, funktioniert es. Was mache ich falsch?
 

mrBrown

Super-Moderator
Mitarbeiter
Der Code sieht auf den ersten Blick nicht falsch aus, der Fehler wird vermutlich an anderer Stelle liegen, kannst du etwas Kontext zeigen?
 

Ginso89

Mitglied
Also,
ich teste gerade die smartlinkdevice Bibliothek aus.
Aus meinem Manifest:
Code:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".LockScreenActivity"/>
    <service android:name=".SdlService"/>
    <receiver android:name=".SdlReceiver">
        <intent-filter>
            <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
            <action android:name="android.media.AUDIO_BECOMING_NOISY" />
        </intent-filter>
    </receiver>
</application>

mein Receiver:
Code:
public class SdlReceiver  extends BroadcastReceiver {    
   public void onReceive(Context context, Intent intent) {
      final BluetoothDevice bluetoothDevice = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

      Log.d("Receiver", intent.getAction());
    
      // if SYNC connected to phone via bluetooth, start service (which starts proxy)
      if (intent.getAction().compareTo(BluetoothDevice.ACTION_ACL_CONNECTED) == 0) {
         SdlService serviceInstance = SdlService.getInstance();
         if (serviceInstance == null) {
            Intent startIntent = new Intent(context, SdlService.class);
            startIntent.putExtras(intent);
            context.startService(startIntent);
         }
      }
      else if (intent.getAction().equals(android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
         // signal your service to stop playback
      }
   }
}

und im Service
Code:
public class SdlService extends Service implements IProxyListenerALM {
    private SdlProxyALM proxy = null;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            startProxy();
        }
        return START_STICKY;
    }

   public void startProxy() {
       if (proxy == null) {
        try {
              proxy = new SdlProxyALM(this, this, APP_NAME, true, Language.DE_DE, Language.DE_DE, APP_ID);
          } catch (SdlException e) {
               e.printStackTrace();
               // error creating proxy, returned proxy = null
               if (proxy == null) {
                   stopSelf();
               }
           }
       }
   }
 
  @Override
  public void onOnHMIStatus(OnHMIStatus notification) {
   
     if(!notification.getHmiLevel().equals(HMILevel.HMI_NONE)
             && firstNonHmiNone){
         sendCommand();
     }
  }

   public void sendCommand() {
     AddCommand command = new AddCommand();
     MenuParams params = new MenuParams();
     params.setMenuName("Test");
     command.setCmdID(0);
     command.setMenuParams(params);
     command.setVrCommands(Arrays.asList(new String[]{"TEST"}));
     sendRpcRequest(command);
  }

    @Override
    public void onOnCommand(OnCommand notification) {
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
               && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000, 10, new LocationListener() {
          ...
       }
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
L Eingaben in der MainActivity einem Service übergeben Android & Cross-Platform Mobile Apps 0
AllBlack Auf der Suche nach einem App-Entwickler Android & Cross-Platform Mobile Apps 1
I Android Auf ImageView aus einem anderen Thread zugreifen liefert Fehlermeldung (App stürzt ab) Android & Cross-Platform Mobile Apps 5
ruutaiokwu Android In einem Android-“Spinner”-Element GLEICHZEITIG Bild (links) UND Text (rechts) anzeigen Android & Cross-Platform Mobile Apps 0
ruutaiokwu Wie fügt man bei Android Studio .jar-Libraries zu einem Android-Java-Projekt hinzu? Android & Cross-Platform Mobile Apps 33
M mehrere URLs in einem AsyncTask abarbeiten Android & Cross-Platform Mobile Apps 2
M Android App → Problem mit dem Speichern von einem Bitmap–Objekt. Android & Cross-Platform Mobile Apps 1
Alex/89 ImageView mit einem Bild von der SD Karte füllen Android & Cross-Platform Mobile Apps 5
J wie sicher ist der Quellcode in einem apk? bzw wie schützt man ihn? Android & Cross-Platform Mobile Apps 6
B Android In einem View der ersten Activity zweite anzeigen Android & Cross-Platform Mobile Apps 2
A Android Von einem Thread auf anderen zugreifen Android & Cross-Platform Mobile Apps 3
W Android Bestimmen von welchem Typ die Objekte in einem ArrayAdapter sind Android & Cross-Platform Mobile Apps 3
X Android Befehle an einem PC-Programm schicken Android & Cross-Platform Mobile Apps 4
N Textview macht immer nach einem Beistrich einen Abstand Android & Cross-Platform Mobile Apps 6
S Android binäre Daten zwischen Android und einem Java-Server Android & Cross-Platform Mobile Apps 5
B Programm aus einem Buch funzt nicht! Android & Cross-Platform Mobile Apps 16
B Zeilen zählen in einem Textfile Android & Cross-Platform Mobile Apps 1
G Schriftgröße in einem TextField ändern Android & Cross-Platform Mobile Apps 2
A Problem beim Subtrahieren eines Double von einem Double Android & Cross-Platform Mobile Apps 5
U ein texteingabefeld auf einem Canvas . Android & Cross-Platform Mobile Apps 2
M Java Midlet -> in einem ChoiceGroup ein TextField zur Wah Android & Cross-Platform Mobile Apps 2
G Java Programme auf einem PDA Android & Cross-Platform Mobile Apps 1
W Wieso gehen Log nicht im Service? Android & Cross-Platform Mobile Apps 20
W Background Service Daten verarbeiten/Schleife ect. Android & Cross-Platform Mobile Apps 1
L Unzuverlässiger Service Android & Cross-Platform Mobile Apps 1
J Service starte nicht mehr Android & Cross-Platform Mobile Apps 13
J intend Service im Android Studio Android & Cross-Platform Mobile Apps 4
L Prüfen ob Service läuft Android & Cross-Platform Mobile Apps 3
N Android Game Background Service Android & Cross-Platform Mobile Apps 11
Excess Android Service läuft nicht in Sandby weiter Android & Cross-Platform Mobile Apps 2
D Java ME Bild vom Web-Service aus zurück geben Android & Cross-Platform Mobile Apps 8
T Android Datenbankverbindung in Service für Push Notification Android & Cross-Platform Mobile Apps 0
G Thread in einer Service erstellen Android & Cross-Platform Mobile Apps 0
S Android In Service überprüfen ob eine Activity gebunden ist? Android & Cross-Platform Mobile Apps 6
S Android Kommunikation zwischen UI -> Service -> Thread Android & Cross-Platform Mobile Apps 4
C Android Kommunikation zwischen Service und Activity Android & Cross-Platform Mobile Apps 8
D Android selben Service mehrmals Aufrufen Android & Cross-Platform Mobile Apps 17
L Android Zugriff aus unbound Service auf Preferences Android & Cross-Platform Mobile Apps 2
S Htmlcode abfragen und ausgeben Android & Cross-Platform Mobile Apps 25
B Android Abfragen wie lang ein Button gedrückt wurde Android & Cross-Platform Mobile Apps 2
S Windows-Tablet: Tablet horizontal/vertikal abfragen Android & Cross-Platform Mobile Apps 2
A Android USB abfragen Android & Cross-Platform Mobile Apps 6
K IMEI abfragen Android & Cross-Platform Mobile Apps 8
C Joystick abfragen Android & Cross-Platform Mobile Apps 2

Ähnliche Java Themen

Neue Themen


Oben