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

12
  • Can you explain me reason for downvote. Commented Jan 17, 2018 at 8:08
  • Hi you can create interface and set that as a listener on activity class and send callbacks from service class. Commented Jan 17, 2018 at 8:18
  • @PankajKantPatel Thank you sir..can you give some reference link please..how to set and get Commented Jan 17, 2018 at 8:20
  • Please look into bind service code and then use binder object to get instance of service object.i don't have code examples yet Commented Jan 17, 2018 at 8:23
  • where do you put LocalBroadcastManager.getInstance(this).registerReceiver( mMessageReceiver, new IntentFilter("GPSLocationUpdates")); ? Commented Jan 17, 2018 at 8:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

D thank you for your suggestion sir..i will try it..i already start bind service
In the Activity's onDestroy() method, don't forget to call unbindService(mConnection)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.