Я определил службу с именем LocationService
, которая отправляет намерение через LocalBroadcastManager
следующим образом:
Intent intent = new Intent(ACTION_LOCATION_BROADCAST);
intent.putExtra(EXTRA_LATITUDE, lat);
intent.putExtra(EXTRA_LONGITUDE, lng);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
В моем MainActivity1
я получил широту и долготу с помощью следующего кода, который работает нормально
LocalBroadcastManager.getInstance(this).registerReceiver(
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
mlattitude = Double.parseDouble(intent.getStringExtra(LocationService.EXTRA_LATITUDE));
mlongitude = Double.parseDouble(intent.getStringExtra(LocationService.EXTRA_LONGITUDE));
}
}, new IntentFilter(LocationService.ACTION_LOCATION_BROADCAS));
Теперь я запустил этот сервис в другом действии (скажем, MainActivity2
), вот код:
LocationService
public class LocationService extends Service implements LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener{
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
public LocationService getService() {
return LocationService.this;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){ ... }
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onConnected(Bundle dataBundle) { ... }
@Override
public void onLocationChanged(Location location) {
sendMessageToUI(String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()));
}
}
private void sendMessageToUI(String lat, String lng) {
Log.d(TAG, "Sending info...");
Intent intent = new Intent(ACTION_LOCATION_BROADCAST);
intent.putExtra(EXTRA_LATITUDE, lat);
intent.putExtra(EXTRA_LONGITUDE, lng);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
MainActivity1
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocalBroadcastManager.getInstance(this).registerReceiver(
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive: called");
mlattitude = Double.parseDouble(intent.getStringExtra(LocationService.EXTRA_LATITUDE));
mlongitude = Double.parseDouble(intent.getStringExtra(LocationService.EXTRA_LONGITUDE));
if (mlattitude != null && mlongitude != null) {
mLatlngTxtVu.setText("Latitude : " + mlattitude + "Longitude: " + mlongitude);
}
}
}, new IntentFilter(LocationService.ACTION_LOCATION_BROADCAS));
MainActivity1 работает нормально и задает текстовую широту и долготу.
MainActivity2
public class MainActivity2 extends AppCompatActivity {
// Service related declaration
LocationService mLocationService;
boolean mBound = false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2_main);
Intent serviceIntent = new Intent(this, LocationService.class);
Log.d(TAG, "onCreate: intent started");
startService(serviceIntent);
LocalBroadcastManager.getInstance(this).registerReceiver(
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive: called");
mlattitude = Double.parseDouble(intent.getStringExtra(LocationService.EXTRA_LATITUDE)); // Getting NullPointer Error here
mlongitude = Double.parseDouble(intent.getStringExtra(LocationService.EXTRA_LONGITUDE));
Log.d("latlng", "onReceive: " + mlongitude + mlattitude);
if (mlattitude != null && mlongitude != null) {
mLatlngTextVu.setText("Latitude : " + mlattitude + "Longitude: " + mlongitude);
}
}
}, new IntentFilter(LocationService.ACTION_LOCATION_BROADCAST));
}
Я получаю исключение нулевого указателя в mlatitude в методе onRecieve () MainActivity2.
Пожалуйста, помогите и скажите мне, что я делаю не так.