Широковещательный приемник из службы в MainActivity / MapsActivity
У меня есть служба, которая получает текущую позицию каждую секунду, а затем служба должна отправить местоположение с трансляцией обратно в основной вид деятельности. С помощью журнала я вижу, что класс обслуживания отправляет широковещательную рассылку, но Main Activity никогда не получает ее.
Мой сервис:
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);
}
[...]
}
}
Основной вид деятельности (с картой в комплекте):
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) {...}
}
Надеюсь, вы можете помочь