2016-03-08 24 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