-2

I have Three class

1.MenuActivity

2.LocationUpdateService

3.MultipleMarker

1.MenuActivity

@Override
    protected void onStart() {
        super.onStart();

        bindService(new Intent(this, LocationUpdatesService.class), mServiceConnection,
                Context.BIND_AUTO_CREATE);
    }

2.LocationUpdateService:(This is Service Class)

   private  void sendMessageToActivity(Location l, String msg) {
        Intent intent = new Intent("GPSLocationUpdates");
        // You can also include some extra data.
        intent.putExtra("Status", msg);
        Bundle b = new Bundle();
        b.putParcelable("Location", l);
        intent.putExtra("Location", b);
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
            Toast.makeText(context,"Sending Data to BroadCast Receiver",Toast.LENGTH_LONG).show();
    }

3.MultipleMarker(Activity)

 LocalBroadcastManager.getInstance(this).registerReceiver(
                mMessageReceiver, new IntentFilter("GPSLocationUpdates"));



 private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Get extra data included in the Intent
            String message = intent.getStringExtra("Status");
            Bundle b = intent.getBundleExtra("Location");

            Log.d("messagesshow","****** "+message);

           Location   lastKnownLoc = (Location) b.getParcelable("Location");
            if (lastKnownLoc != null) {

                Log.d("messagesshow","*****    "+String.valueOf(lastKnownLoc.getLatitude()));

            }

             Toast.makeText(context, "Data Receiver called", Toast.LENGTH_SHORT).show();
        }
    };

My Problem is that : When i open My MenuActivity My Toast Messages printing Sending Data to BroadCast Receiver then onclick of Button I am calling MultipleMarker.class there i am not able to getting the values from service...But when i press back button i redirect to MenuActivity at that time i getting values...

I want to get data from my LocationUpdateservice class into in my MultipleMarker.class activity...

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54

1 Answers1

2

You can create an Interface and get your Service's reference following these steps:

In your LocationUpdateService add those attributes:

public class LocationUpdateService extends Service{

    // The Binder is an intern class
    public class LocalBinder extends Binder {
        //The Binder as a method which return the service
        public LocationUpdateService getService() {
            return LocationUpdateService.this;
        }
    }

    private final IBinder mBinder = new LocalBinder();

}   

Then in your MenuActivity:

public class MenuActivity extends AppCompatActivity {
    private LocationUpdateService mService;

    // Connection interface to the service
    private ServiceConnection mConnection = new ServiceConnection() {

        // Called when the activity connects to the service
        public void onServiceConnected(ComponentName className, IBinder service) {

        // Here you get the Service
            mService = ((LocationUpdateService.LocalBinder)service).getService();

        }

        // Called when service is disconnected
        public void onServiceDisconnected(ComponentName className) {
            mService = null;
        }
    };
}

Then your bindService() method will call the onServiceConnected() and you will get the instance of your Service.

Note: onServiceConnected() is called asynchronously, so you can't use Service's methods right after calling bindService() because you'll have a NullPointerException.

Diego D.
  • 398
  • 3
  • 8