I'm binding to Service by this way:
Activity class:
ListenLocationService mService;
@Override
public void onCreate(Bundle savedInstanceState) {
...
Intent intent = new Intent(this, ListenLocationService.class);
intent.putExtra("From", "Main");
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
...
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
}
public void onServiceDisconnected(ComponentName arg0) {
}
};
This is onBind
method of my Service:
@Override
public IBinder onBind(Intent intent) {
Bundle extras = intent.getExtras();
if(extras == null)
Log.d("Service","null");
else
{
Log.d("Service","not null");
String from = (String) extras.get("From");
if(from.equalsIgnoreCase("Main"))
StartListenLocation();
}
return mBinder;
}
So I have "null" in LogCat
- bundle is null in spite of I made intent.putExtra
before bindService
In general Service works fine. However I need to call StartListenLocation();
only from main activity of application (I decide to do this by sending a flag).
How can I send a data to service?Or maybe there's another way to check what activity launched onBind
?
You can pass the parameter in this simple way:-
and get the parameter in onStart method of your service class
Enjoy :)
According to this session http://www.google.com/events/io/2010/sessions/developing-RESTful-android-apps.html
One way of sending data to a service is by using a database containing of columns pid(process id),data,status where the status can be READ , READY , BINDING and so on , when the service is load it reads the table which is filled by the calling activity and can be deleted when finished , the other way indicated is AIDL as indicated in the previous answer , choose according to your application.
1 Create a interface that declare all method signature that you want to call from a Activity:
2 Make your binder implements ILocaionService and define the actual method body:
3 In activity that bind to the service, reference your binder by the interface:
All communications (i.e. method call to your Service) should be handled and performed via the binder class initialize and return in ServiceConnection.onServiceConnected(), not the actual service class (binder.getService() is unnecessary). This is how bind service communication designed to work in the API.
Note that bindService() is an asynchronous call. There will be a lag after you call bindService() and before ServiceConnection.onServiceConnected() callback get involved by system. So the best place to perform service method is immediately after mService get initialized in ServiceConnection.onServiceConnected() method.
Hope this helps.