I am using location listerner in a service, and i have overriden onLocationChanged(Location) method. Now i want that whenever this method is called for the first time, i want to stop the service. So please tell me how to do it
Call stopSelf from the onLocationChanged method (assuming the Service itself implements that interface).
Related
I have an activity named MainActivity and a service named MainService.
In mainActivity i have a button to start the MainService and a textView(lets say- txt1).
And in OnCreate() method of the service i have taken an instance of MainActivity and using that instance i am trying to update the txt1(TextView) of the activity- using setText. Even tried with post() method.
But its showing error. If i am not wrong than i think UI cant be updated from service. Or, i need to bind the service with the activity. right?
So, my question is that why cant i update textView of activity from service?
you probably can just not directly, if you create a callBack reference you can call a method in your activity from the service, but essentially the textView updates are run on the UI thread, so the actual update has to happen in the activity..
This might be helpful: https://developer.android.com/training/multiple-threads/communicate-ui.html
You can do it this way:
sendBroadcast(intent) from service.
register receiver in your activity's onResume() and unregister the same in onPause().
Update textview in onReceive of BroadcastReceiver.
What I did:
startService in MainActivity's onCreate().
implement button onBindServiceClicked(View v) in MainActivity. So when the button is clicked, an intent with data will be sent to the service.
However, I found this only work once. When I clicked the button the second time, it seems the onBind() was not invoked.
Do I need to call something like unbindService, so that the onBind() will be invoked many times?
If you want to keep sending data via Intent, you should use Activity.startService(intent) instead. Each time your service will have it's onStartCommand() callback invoked.
Usually when I create an Android service I implement the onCreate method, but in my last project this does not work. I tried implementing onStartCommand, and this seems to work.
The question is: when I have to implement a service which method is required? Which methods I have to implement? onCreate, onStartCommand, or both? And what is the role of each?
onCreate() is called when the Service object is instantiated (ie: when the service is created). You should do things in this method that you need to do only once (ie: initialize some variables, etc.). onCreate() will only ever be called once per instantiated object.
You only need to implement onCreate() if you actually want/need to initialize something only once.
onStartCommand() is called every time a client starts the service using startService(Intent intent). This means that onStartCommand() can get called multiple times. You should do the things in this method that are needed each time a client requests something from your service. This depends a lot on what your service does and how it communicates with the clients (and vice-versa).
If you don't implement onStartCommand() then you won't be able to get any information from the Intent that the client passes to onStartCommand() and your service might not be able to do any useful work.
Service behave same like Activity Whatever you want to associate once with a service will go in onCreate like initialization
and whenever the service is called using startService. onStartCommand will be called. and you can pass any action to perform . like for a music player , You can play ,pause,stop using action
And you do any operation in service by sending an action and receiving it on onStartCommand
onCreate work like a Constructor.
Edit in Short
onCreate() calls only for the first time you start a Service Whereas onStartCommand() calls everytime you call the startService again. It let you set an action like play,stop,pause music.
public void onStartCommand()
{
if(intent.getAction.equals("any.play")
{
//play song
}
else if(intent.getAction.equals("any.stop")
{}
}
I am using a service which implements LocationListener class.Now i want to know the current location(Latitude,Longitude) of the device. I have used requestLocationUpdates method so that onLocationChanged method gets called when the location changes. I want that onLocationChanged method should be called only once i.e. when i get the location for the first time, it should not be called again.
I have called stopSelf method to stop the service inside onLocationChanged method. But still i am getting location updates.
Please help me.
Call removeUpdates on LocationManager, passing your location listener.
mLocationManager.removeUpdates(mListener);
How to call method of my Service each time Activity unbinds it? It's desirable to don't call it from ServiceConnection.
What I want to achieve:
I'm using started service, that checks socket's InputStream every second. I want to change this interval to, for example, 1 minute, when application is stoped.
How to call method of my Service each time Activity unbinds it?
Call a method on your Binder before calling unbindService().