3

I have been looking around and this seems to be a Android L related bug that was apparently solved solved using the code I already have.

When I try to call bindService I get:

Fatal Exception: java.lang.RuntimeException: Unable to resume activity {MyActivity}: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.android.vending.billing.InAppBillingService.BINL }

This is the piece of code that causes the crash:

    final Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
    serviceIntent.setPackage("com.android.vending");
    activity.bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);

It only happens on Android 6.0.1 and my target in gradle in 23 and I can't seem to understand what's wrong with that on API 23...

Carlos
  • 147
  • 10

1 Answers1

1

If you are using IabHelper classes. Go to startSetup Method in IabHelper.java. add below code

Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
        if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {
            // service available to handle that Intent
            serviceIntent.setPackage("com.android.vending");

            mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
        }
        else {
            // no service available to handle that Intent
            if (listener != null) {
                listener.onIabSetupFinished(
                        new IabResult(BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE,
                                "Billing service unavailable on device."));
            }
        }

This method will help you turn the implicit intent into the explicit form. Inspired from SO answer: https://stackoverflow.com/a/26318757/1446466 bindServiceConn() method is creating a service.

 * @param context
 * @param implicitIntent - The original implicit intent
 * @return Explicit Intent created from the implicit original intent
 */

public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
    // Retrieve all services that can match the given intent
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);

    // Make sure only one match was found
    if (resolveInfo == null || resolveInfo.size() != 1) {
        return null;
    }

    // Get component info and create ComponentName
    ResolveInfo serviceInfo = resolveInfo.get(0);
    String packageName = serviceInfo.serviceInfo.packageName;
    String className = serviceInfo.serviceInfo.name;
    ComponentName component = new ComponentName(packageName, className);

    // Create a new intent. Use the old one for extras and such reuse
    Intent explicitIntent = new Intent(implicitIntent);

    // Set the component to be explicit
    explicitIntent.setComponent(component);

    return explicitIntent;
}



  protected void bindServiceConn() {
//call this method 
        Intent intent = createExplicitFromImplicitIntent(context.getApplicationContext(), new Intent("com.android.vending.billing.InAppBillingService.BIND"));
        context.bindService(intent, mServiceConn, Context.BIND_AUTO_CREATE);
    }

protected void unbindServiceConn() {
    context.unbindService(mServiceConn);

    context=null;
}
Community
  • 1
  • 1
Sree Reddy Menon
  • 1,301
  • 13
  • 23
  • In the original post we already see a call to `serviceIntent.setPackage("com.android.vending");`. I'm also seeing this error, and I have the `queryIntentServices` check as well as the explicit `setPackage()` call. What's weird is that the error message mentions an intent ending with `BINL` instead of `BIND`. I'm seeing this as well as the original poster. – Carmen Dec 28 '16 at 18:43