# GPS in einem Service abfragen



## Ginso89 (30. Apr 2017)

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


```
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 (30. Apr 2017)

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


----------



## Ginso89 (30. Apr 2017)

Also,
ich teste gerade die smartlinkdevice Bibliothek aus.
Aus meinem Manifest:

```
<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:

```
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

```
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() {
          ...
       }
```


----------

