Why cant i update a textView in Activity - from a service - android

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.

Related

Android service (location listener) to update activity

I have an activity that shows a map with markers on it. Also I have a location listener, whose values will update the activity arraylist and cause a redraw of the markers based on the updated arraylist values.
I want this to always run (even if the activity is put in the background). I understand that Activity may get killed so I expect that I need a service for the location lisntener. Now my question is: How can I update the activity arraylist and redraw contents when the service is actually the one obtaining info?
I read something about boundservice so maybe I need that? However I need the service running even if activity is killed.
OR Do I just create service and then send a broadcast to activity?
Thank you
Like you have written at the end of your post you could use a BroadcastReceiver for this. That's the way I use it.
In onCreate(...) of your Activity, register it locally:
LocalBroadcastManager.getInstance(this).registerReceiver(...);
in the onReceive(...) method of your receiver, update the required data and unregister it in Activity's onDestroy() method, again using the LocalBroadcastManager object.
Needless to say, you send the broadcast signal from your Service's onLocationChanged(...) method, since it is in the same package as your Activity, the LocalBroadcastManager will forward the signal to your Activity.
LocalBroadcastManager.getInstance(this).sendBroadcast(...);

Android onCreate or onStartCommand for starting service

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")
{}
}

How to update textview of paused activity

I'm receiving intents in a broadcast receiver (declared in manifest), details of which I am 'logging' via b'cst intents received by my 'MainActivity', whose receiver updates the contents of a text view and is registered within the MainActivity code.
I'm wondering if there's a way to keep the contents of the MainActivity text view updated even if the MainActivity doesn't have 'focus' (i.e. another activity has been started). I appreciate that the b'cast receiver of the MainActivity will become unregistered upon pausing, but feel there ought to be a way to do this.
Any ideas?
You should do all of that on the "onResume" of the activity, just when it gets to the foreground.
One of solutions is to use a handler inside receiver, and in that you call a static method inside your activity...inside that method you can update or whatever you need to do with your text view...

Updating a TextView in an Activity from a Service in Android?

I have a TextView in an Activity which I wish to update from a service. How can this be achieved?
You could use a BroadcastReceiver. An intent should be fired from a service and TextView.setText() called in the onReceive() method of the BroadcastReceiver.
Yes you can. I would suggest creating some kind of interface that the service feeds updates to. The activity will implement this interface and set itself as a 'listener' on the service (removing itself in onDestroy).

help with binding a local service

I Need some help here, I have a service which I can start or stop whenever I want and using the onStart() command to pass some extras using putExtras() from my activity
But I need some serious basic instructions on how to interact with the already created service.
Please don't refer me to another webpage which already have some implementations, just give me the needed code to interact from my UI activity to the service:
something like this:
public class myActivity extends Activity {
Object ReceivedObjectFromService;
onCreate()
{
some stuff here
myMethod()
}
public class myMethod()
{
//do some stuff with the ReceivedObjectFromService
//Don't know how to call this method from the service btw
}
please some help, I don't understand the tutorials on how to interact service to activity or viceversa
Interaction with already created service is no different to starting a brand new service. You just simply call startService() so your client code is no different.
Now, the part which is different is the service itself. In your service, onCreate() must start a background thread or a timer to carry on doing a work. onStart() will receive all startService cases and must in fact add the data it receives in the Intent to an internal list or queue and then in the timer's callback start processing from this queue.
Now you can pass any messages or data you want (even closing the service) using startService and passing data in the Intent that your service understands.
Hope this helps.

Categories

Resources