2016-03-08 46 views
3

我一直在环顾四周,这似乎是一个Android L相关的错误,显然已经解决了使用我已有的代码解决。API 23(Android 6.0.1)InApp计费:服务意图必须明确:意图

当我尝试调用bindService我得到:

致命异常:了java.lang.RuntimeException:无法恢复活动 {} MyActivity:java.lang.IllegalArgumentException异常:服务意向必须 是明确的:意图{ ACT = com.android.vending.billing.InAppBillingService.BINL}

这是导致崩溃的代码段:

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

它只发生在Android 6.0.1和我在23 gradle产出和目标,我似乎无法理解什么是错的API 23 ...

回答

1

如果您正在使用IabHelper类。前往startSetup方法IabHelper .java。添加以下代码

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.")); 
      } 
     } 

此方法将帮助您将隐式意图转化为显式形式。灵感来自SO回答:https://stackoverflow.com/a/26318757/1446466 bindServiceConn()方法正在创建一个服务。

* @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; 
} 
+0

在原文中,我们已经看到了对'serviceIntent.setPackage(“com.android.vending”);'的调用。我也看到这个错误,并且我有'queryIntentServices'检查以及显式的'setPackage()'调用。奇怪的是,错误消息提到了以'BINL'而不是'BIND'结尾的意图。我看到这个以及原来的海报。 – Carmen