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.
Related
I have one service and two activities. I from one Activity go to another, and in the onStop method, do unbindService.
When I came in the second Activivity the service destroyed.
When the second activity calls the bindservice, a new service is created.
How to make so that the service is not destroyed?
If you want that service should not stop when migrating to next activtiy,then you should use IntentService instead of Service.
InetntService don't have any UI,so it will contiously runs in the background without user interaction and when it finishes then you have to store its data somewhere (or you can use the broadcast reciver )and populate that data in the next actvity if you need.
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.
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
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.
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")
{}
}