Updating a TextView in an Activity from a Service in Android? - 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).

Related

Call Activity's method on notification

I have an Activity with 2 buttons. A play and a pause button.
When this activity gets in the background, which means when the onStop() method has bean called, this creates a permanent notification that is only destroyed when the activity is resumed.
The notification does provide the play and pause buttons too. How do I call the activity's play() and pause() methods when the notification's buttons are clicked?
I really have no clue on how to address that issue.
I'm using a RemoteViews to construct a custom layout. And I know you can call
setOnClickPendingIntent() to bind an Intent to a view.
big thanks.
Instead of calling activity methods, you can use a service to initialize a media player instance and play or pause by passing intent extras. This way you would have more and easier control over the player.
I followed your advise by writting a Service that receives all input from The Notification. I then use the LocalBroadcastManager to send appropriate message to a BroadcastReceiver, which is responsible for updating the notification. The Activity also has a inner BroadcastReceiver. That way both receivers receive intent from the Service and can update their UIs independently.

How to hide status bar in service class?

I need to use the following line in service class
getWindow().getDecorView().setSystemUiVisibility(8);
Please anyone help.
A service can not set window properties, since it has no view/UI. You must send an event to your activity, which must set this property. When it comes to sending the event to activity, you can use several mechanisms:
Use a messenger which you passed from activity to service earlier
Use AIDL interface
Easiest : Send a broadcast Intent from your Service class, let your Activity listen for that broadcast, and set the action on UI thread

Is it possible to trigger the onclick event of activity in background service?

For example, I started an activity, there is a button in the activity
And then I start the background service, this service will check whether the target activity is on foreground, then trigger the click event of that button.
Is it possible to do this?
Why would you trigger a click event of an activity rather than notifying the activity about its results? Try to use local broadcasts which are sent from the service to the activity. The activity registers for that broadcast and in its onReceive of the BroadcastReceiver you'll trigger your onClick method or any other method of your choice. The Receiver is registered in onResume and unregistered in onPause to guarantee that the activity is actually visible.
I would not recommend to use a direct dependency on your activity as this might cause IllegalStateExceptions if in any circumstance your activity is not started or visible at all.
I can strongly recommend you to use eventbus concept like "otto by square" in this case.
you will subscribe to event from the activity this will keep to modularity and will let you do this function

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

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.

Bind an AppWidget to a Service

I implemented a service and I bind my activities in the app to it.. How can I bind an AppWidget too? I need to call some service's method from AppWidget's buttons...
Thanks in advance

Categories

Resources