如果您正在使用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;
}
在原文中,我們已經看到了對'serviceIntent.setPackage(「com.android.vending」);'的調用。我也看到這個錯誤,並且我有'queryIntentServices'檢查以及顯式的'setPackage()'調用。奇怪的是,錯誤消息提到了以'BINL'而不是'BIND'結尾的意圖。我看到這個以及原來的海報。 – Carmen