Dealing with L Deprecations: bindService()
In my post noting API changes in “L”,
I mentioned that bindService()
now no longer supports implicit Intent
s. Unfortunately,
some things require that at present… such as
Google’s in-app purchasing (IAP) API.
(another hat tip to Alex Curran for pointing that out)
The real solution is for anyone exposing APIs that require service binding to move to
explicit Intent
s, perhaps employing a wrapper JAR that is the actual API to hide
those service details. In the case of IAP, Google should talk to Google to learn what Google’s
expectations are in this area, so Google can modify Google’s code to work with Google’s code.
However, depending on how quickly Google talks to Google and can ship a new version of the
Google IAP API, you may have to deal with this more yourself.
The primary workaround is to simply not raise your targetSdkVersion
past 19, as this
behavior should only kick in at that point. But, that then inhibits your ability to work
with some of the “L” stuff for testing and preparing to take advantage of the new, um, materials.
If you need to bind to a service and you need to convert an implicit Intent
to an
explicit Intent
, use PackageManager
. The simple solution is to use resolveService()
,
given the implicit Intent
. That will tell you the “best service” for the implicit Intent
.
You can then call setComponentName()
on the implicit Intent
to make it explicit, using
the information in the ResolveInfo
returned by resolveService()
. A better approach is
to use queryIntentServices()
and confirm that the list returns only one entry —
if not, somebody else is trying to intercept your IAP calls. And an even better approach
would be to employ some signature pinning,
to ensure that you’re talking to the real IAP… but that’s a security measure,
not one specifically dealing with the “L” bindService()
issue.
Note that you really want to convert the implicit Intent
to an explicit one by adding
the ComponentName
. For all you know, the service in question actually looks at the
action string, and so constructing a new explicit Intent
without that action string would
perhaps cause problems.
Note that I have not personally tried all aspects of this technique, let alone on “L”, and so your kilometerage may vary. That being said, it’s how I would approach the problem, assuming that I was stuck in this situation.