cfg
cfg

Reputation: 1

Broadcast Receiver from Service in MainActivity / MapsActivity

I have a service, which get the current position every Second and then the Service should send the location with an Broadcast back to the Main Activity. With the log i can see, that the Service Class sends the Broadcast, but the MainActivity never get it.

My Service:

public class MyService extends Service
{
private static final String TAG = "BOOMBOOMTESTGPS";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10;
private void sendLocation(Location l) {
    Intent intent = new Intent("GPSLocationUpdates");
    // You can also include some extra data.
    intent.putExtra("Lat", l.getLatitude());
    intent.putExtra("Lon", l.getLongitude());
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    Log.e(TAG, l + "gesendet!");

}

private class LocationListener implements android.location.LocationListener
{
    Location mLastLocation;

    public LocationListener(String provider)
    {
        Log.e(TAG, "LocationListener " + provider);
        mLastLocation = new Location(provider);
    }

    @Override
    public void onLocationChanged(Location location)
    {
        Log.e(TAG, "onLocationChanged: " + location);
        Toast.makeText(MyService.this, "ja", Toast.LENGTH_LONG).show();
        sendLocation(location);
        mLastLocation.set(location);
    }

    [...]
}
}

The MainActicity (with Map included):

public class MainActivity extends AppCompatActivity
    implements OnMapReadyCallback, 
    NavigationView.OnNavigationItemSelectedListener {

private static final String TAG = "Location";
private GoogleMap mMap;
private final static int MY_PERMISSIONS_FINE_LOCATION = 123;
track track = new track();


private BroadcastReceiver locationReceiver = new BroadcastReceiver() {

    @Override

    public void onReceive(Context context, Intent intent) {
        Log.e(TAG, "onReceive");

        String action = intent.getAction();
        LatLng pos = new LatLng(intent.getDoubleExtra("Lat", 0), 
intent.getDoubleExtra("Lon", 0));

        Log.e(TAG, "Position: " + pos + "empfangen");
        track.posAdd(pos);
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

LocalBroadcastManager.getInstance(this).registerReceiver(locationReceiver, 
new IntentFilter("GPSLocationUpdates"));
    Log.i(TAG, "BroadcastManager erstellt");
    setContentView(R.layout.activity_main);
    // Obtain the SupportMapFragment and get notified when the map is ready 
to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) 
getSupportFragmentManager()
            .findFragmentById(R.id.map);
    [...]

}

public void onMapReady(GoogleMap googleMap) {
    [...]
    startService(new Intent(this, MyService.class));
}


@Override
public void onRequestPermissionsResult(int requestCode, String 
permissions[], int[] grantResults) {....}

public void onBackPressed() {...}

@Override
public boolean onCreateOptionsMenu(Menu menu) {...}

@Override
public boolean onOptionsItemSelected(MenuItem item) {...}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {...}
}

Hopefully you can help

Upvotes: 0

Views: 205

Answers (0)

Related Questions