When should you use unbindService () and how to use it to unbind a remote service using the AIDL interface?

I am writing a simple music player, and I created a playback service that implements the AIDL interface for binding to clients, one simple track browser, and the other an even simpler gameplay. A service manages a MediaPlayer object, while two actions use ServiceConnections to obtain connections to the service.

This is included in the onStart () methods for both activities:

@Override public void onStart() { super.onStart(); Intent i = new Intent(this, PureService.class); startService(i); bindService(i, mConnection, 0); } 

I did this so that the service did not immediately stop at unleashing. Of course, this is actually not a problem, because my activity does not refuse service at all. Anytime my application receives an unbindService in any of these actions, an unbindService throws an IllegalArgumentException every time, without exception (hehe).

In onStop methods:

 @Override public void onStop() { super.onStop(); if (mBound) { try { unbindService(mConnection); } catch (java.lang.IllegalArgumentException e) { //Print to log or make toast that it failed } } mBound = false; } 

What interests me is:

  • Should I call unbindService () in the onStop () method? Or even?
  • Did I call him right?
  • Is there anything special about how I start / link a service that I should be aware of?
  • Am I doing something completely, completely wrong? I am new to Android programming, so of course it is possible.

Thanks in advance.

EDIT: ServiceConnection Overrides

 public void onServiceConnected(ComponentName className, IBinder service) { mBound = true; mService = IPureService.Stub.asInterface(service); } public void onServiceDisconnected(ComponentName arg0) { mBound = false; } 

There is some additional code in the game, but it is not related to the binding itself.

+4
source share
3 answers

Firstly, if you really do not need to make calls to this service through processes (that is, from other .apks or you use android: a process for some reason splits your own .apk into several processes), then I really recommend just abandoning use of help. It is harder without a win. The "Service documentation" in the "Service documentation" shows how to do this: http://developer.android.com/reference/android/app/Service.html

Secondly, binding at the same time as the start is a strong sign of some basic design flaw. Starting a service and binding to a service are semantically different from each other, so this will be done in different places based on these different semantics. That is, if both of them are even made at all ... in fact, this is an unusual situation when you use both start and bind with the same service.

In the implementation of the class to perform music playback, it will use start when it is actively performing playback (therefore, its process will not be killed by the system when the user will no longer actively interact with the user interface of the application). Starting the service when the user enters the user interface can cause pain, because now the state of starting / stopping the service is not clearly defined - it can be started either because it is playing, or because the user accidentally hit the user interface of the application, and now, when is the right time to stop it? It will be troublesome.

Now that you need to unlink, you just need to make sure that you always map unbindService () to the previous bindService (). From your code snippets, it looks like you are doing this, but there are strange things in it, for example, mBound is never installed. In fact, if you constantly bind to onStart () and not bind to onStop (), you never need to have mBound to decide whether to unbind, because onStop () is always called after onStart ().

So, with the code you provide here, this does not seem to be a problem. However, if you get exceptions, it is obvious that this may be elsewhere in your application. To reduce the problem, you can use this flag when you call bindService () to get more information in the log when a crash occurs: http://developer.android.com/reference/android/content/Context.html#BIND_DEBUG_UNBIND

+4
source

A few points:

  • Return START_STICKY to onStartCommand if you want your service to live longer than related activities.
  • unbindService () in onStop is fine: this is where I call it in multiple applications and I never saw this particular error. I expect that you have another problem with your ServiceConnection: show the code for your mConnection object, and we can perhaps find out what is wrong with it.
0
source

I also had this strange mistake. Then I tried to bind to the service in onResume (), not in onStart () in Activity, and voila, no exceptions! I still don't know a bit why this works. If someone could explain, I would be one happy coder. :)

0
source

All Articles